Pandas version checks

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

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

  • [X] 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 : bb1f651536508cdfef8550f93ace7849b00046ee 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

Comment From: roib20

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.

Comment From: roib20

take

Comment From: jbrockmendel

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 (https://github.com/pandas-dev/pandas/issues/25288#issuecomment-940698030, #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

Comment From: roib20

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

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.

Comment From: phofl

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