Skip to content
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

BUG: TypeError when attempting to replace Nullable integer data type with a float value #45729

Closed
3 tasks done
roib20 opened this issue Jan 31, 2022 · 5 comments
Closed
3 tasks done
Labels
Bug Closing Candidate May be closeable, needs more eyeballs NA - MaskedArrays Related to pd.NA and nullable extension arrays Needs Discussion Requires discussion from core team before further action replace replace method

Comments

@roib20
Copy link
Contributor

roib20 commented Jan 31, 2022

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd


ser = pd.Series([1], dtype="Int64")
try:
    assert ser.replace({1: 1.1}).to_list() == [1.1]
except TypeError:
    print("In pandas 1.4.0 the printed error is:\n"
          "TypeError: cannot safely cast non-equivalent float64 to int64\n")
    print("In pandas main branch the printed error is:\n"
          "TypeError: Invalid value '1.1' for dtype Int64")

Issue Description

When using any of the pandas Nullable integer data types (Int8Dtype, Int16Dtype, Int32Dtype, Int64Dtype, UInt8Dtype, UInt16Dtype, UInt32Dtype, UInt64Dtype) as a dtype for values in a Series or DataFrame, then attempting to replace that value with a float value, the type casting fails under pandas 1.4.0, and also fails under the current main branch with a slightly different TypeError.

I wasn't sure whether this was intended behavior at first (possibly designed to encourage explicit type casting instead of implicit). But after further testing I think this is indeed a bug because this TypeError does not occur under pandas 1.3.5. Nor does this TypeError occur when using other integer data types such as standard Python int or numpy dtypes like int32 and int64. In all those cases, the type casting is done implicitly without error, and the final value is 1.1 of dtype float64.

Expected Behavior

The Nullable integer value should be replaced with new value and implicitly be type cast as a float.

Installed Versions


commit : bb1f651
python : 3.10.2.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.22000
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 10, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 1.4.0
numpy : 1.22.1
pytz : 2021.3
dateutil : 2.8.2
pip : 21.1.2
setuptools : 57.0.0
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
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None

Suggested Labels

Regression, replace, Dtype Conversions

@roib20 roib20 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 31, 2022
@roib20
Copy link
Contributor Author

roib20 commented Jan 31, 2022

I'm working on a pull request designed to solve both this issue and #40472, by enforcing implicit type casting in some instances of calling the replace method. However I would like to know first whether these changes in behavior are regressions or actually intended. I'm still not sure.

@roib20
Copy link
Contributor Author

roib20 commented Jan 31, 2022

take

@jbrockmendel
Copy link
Member

However I would like to know first whether these changes in behavior are regressions or actually intended. I'm still not sure

I don't think we have completely decided on this matter. @jorisvandenbossche has advocated non-casting behavior for __setitem__-like ops (#25288 (comment), #39584) and I've advocated having casting logic be consistent between methods #45153.

I'd suggest weighing in on the discussion in #39584, #24020, #45153, #25288

@mroeschke mroeschke added NA - MaskedArrays Related to pd.NA and nullable extension arrays Needs Discussion Requires discussion from core team before further action and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 10, 2022
@simonjayhawkins simonjayhawkins added the replace replace method label Mar 20, 2022
@roib20 roib20 removed their assignment Mar 20, 2022
@roib20
Copy link
Contributor Author

roib20 commented Mar 20, 2022

I tested again and can confirm this issue is still present in pandas 1.4.1 as well as current main branch (tested commit afec0e9).

pandas 1.4.1 yields the same TypeError as 1.4.0: TypeError: cannot safely cast non-equivalent float64 to int64
Whereas in main branch the TypeError is: TypeError: Invalid value '1.1' for dtype Int64

I realize this is behavior is still under discussion so not sure what the appropriate solution should be at this point.

@phofl
Copy link
Member

phofl commented Dec 9, 2022

I propose closing here to keep the discussion together In the other issues since this is larger than just replace

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Closing Candidate May be closeable, needs more eyeballs NA - MaskedArrays Related to pd.NA and nullable extension arrays Needs Discussion Requires discussion from core team before further action replace replace method
Projects
None yet
Development

No branches or pull requests

5 participants