Skip to content

Object dtype gives ValueError in _maybe_cast_with_dtype #32413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
david33n opened this issue Mar 3, 2020 · 8 comments · Fixed by #45870
Closed

Object dtype gives ValueError in _maybe_cast_with_dtype #32413

david33n opened this issue Mar 3, 2020 · 8 comments · Fixed by #45870
Assignees
Labels
Dtype Conversions Unexpected or buggy dtype conversions good first issue Needs Tests Unit test(s) needed to prevent regressions
Milestone

Comments

@david33n
Copy link

david33n commented Mar 3, 2020

import pandas as pd
import numpy as np

frame = pd.DataFrame(index=pd.Index([1, np.nan]))
fixed_index = frame.index.where(frame.index.notna(), 'a' + frame.reset_index().index.astype(str))

Traceback (most recent call last):
  File "<input>", line 4, in <module>
  File "C:\Users\Miniconda3\envs\testbed_dtype\lib\site-packages\pandas\core\indexes\base.py", line 3791, in where
    return self._shallow_copy_with_infer(values, dtype=dtype)
  File "C:\Users\Miniconda3\envs\testbed_dtype\lib\site-packages\pandas\core\indexes\base.py", line 567, in _shallow_copy_with_infer
    return Index(values, **attributes)
  File "C:\Users\Miniconda3\envs\testbed_dtype\lib\site-packages\pandas\core\indexes\base.py", line 385, in __new__
    data = _maybe_cast_with_dtype(data, dtype, copy)
  File "C:\Users\Miniconda3\envs\testbed_dtype\lib\site-packages\pandas\core\indexes\base.py", line 5457, in _maybe_cast_with_dtype
    data = data.astype(dtype)
ValueError: could not convert string to float: 'a1'

Problem description

Data is being cast to incorrect dtype based on inferred dtype and gives a ValueError.
Issue was not present in pandas==0.25.3

Expected Output

With pandas==0.25.3

fixed_index
Index([1.0, 'a1'], dtype='object')

Output of pd.show_versions()

python : 3.7.6.final.0
pandas : 1.0.1
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 45.2.0.post20200210
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : 3.0.3
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : 1.3.0
xlsxwriter : None
numba : None

@jbrockmendel jbrockmendel added Bug Constructors Series/DataFrame/Index/pd.array Constructors labels Mar 17, 2020
@jbrockmendel
Copy link
Member

Looks like the issue is in Index.where, shouldnt pass dtype to shallow_copy_with_infer

@simonjayhawkins
Copy link
Member

Issue was not present in pandas==0.25.3

regression in #27883 (i.e. 1.0.0)

e47362a is the first bad commit
commit e47362a
Author: jbrockmendel jbrockmendel@gmail.com
Date: Tue Aug 13 05:20:40 2019 -0700

CLN: Index.__new__ (#27883)

cc @jbrockmendel

@simonjayhawkins simonjayhawkins added Index Related to the Index class or subclasses Regression Functionality that used to work in a prior pandas version and removed Bug labels May 1, 2020
@mroeschke mroeschke added the Bug label May 5, 2020
@cgarciae
Copy link

I tested against master (895f0b4) and the issue is now gone, I think this can be closed now.

@jreback
Copy link
Contributor

jreback commented Apr 11, 2021

we would take a PR with a validation test

@cgarciae
Copy link

I see, I can add a test for this.

@cgarciae
Copy link

take

@simonjayhawkins
Copy link
Member

I tested against master (895f0b4) and the issue is now gone, I think this can be closed now.

also seems fixed on 1.2.x (fixed in 1.2.0)

@simonjayhawkins simonjayhawkins added this to the 1.3 milestone Apr 12, 2021
@simonjayhawkins simonjayhawkins added Needs Tests Unit test(s) needed to prevent regressions and removed Bug Constructors Series/DataFrame/Index/pd.array Constructors Index Related to the Index class or subclasses Regression Functionality that used to work in a prior pandas version labels May 25, 2021
@simonjayhawkins simonjayhawkins removed this from the 1.3 milestone Jun 11, 2021
@rendner
Copy link
Contributor

rendner commented Feb 8, 2022

take

rendner added a commit to rendner/pandas that referenced this issue Feb 8, 2022
rendner added a commit to rendner/pandas that referenced this issue Feb 8, 2022
@jreback jreback added this to the 1.5 milestone Feb 9, 2022
@jreback jreback added the Dtype Conversions Unexpected or buggy dtype conversions label Feb 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Dtype Conversions Unexpected or buggy dtype conversions good first issue Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
7 participants