BUG: dataframe.replace does not raise error when trying to replace pd.NA with np.nan for pd.Float64Dtype (original) (raw)
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 import numpy as np a=pd.DataFrame([[1.,0.],[np.nan, pd.NA],[1.,1.]]) a[1]=a[1].astype(pd.Float64Dtype()) a.replace(a.values[1,1], np.nan)
Issue Description
The a.replace in the example above should raise an error instead of silently doing nothing, as the pd.NA is not replaced by np.nan.
This can lead to bugs in code with casting - for example:
a=pd.DataFrame([[1.,0.],[np.nan, pd.NA],[1.,1.]])
a.replace(a.values[1,1], np.nan).values.astype(float)
gives a numpy array, while
a=pd.DataFrame([[1.,0.],[np.nan, pd.NA],[1.,1.]])
a[1]=a[1].astype(pd.Float64Dtype())
a.replace(a.values[1,1], np.nan).values.astype(float)
raises an error.
Expected Behavior
import pandas as pd
import numpy as np
a=pd.DataFrame([[1.,0.],[np.nan, pd.NA],[1.,1.]])
a[1]=a[1].astype(pd.Float64Dtype())
a.replace(a.values[1,1], np.nan)
should raise an appropriate error.
Installed Versions
INSTALLED VERSIONS
commit : ba1cccd
python : 3.11.5.final.0
python-bits : 64
OS : Darwin
OS-release : 22.5.0
Version : Darwin Kernel Version 22.5.0: Thu Jun 8 22:22:22 PDT 2023; root:xnu-8796.121.3~7/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 2.1.0
numpy : 1.24.3
pytz : 2022.7
dateutil : 2.8.2
setuptools : 68.0.0
pip : 23.2.1
Cython : 3.0.0
pytest : None
hypothesis : None
sphinx : None
blosc : 1.11.1
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.15.0
pandas_datareader : None
bs4 : None
bottleneck : 1.3.5
dataframe-api-compat: None
fastparquet : None
fsspec : 2023.9.0
gcsfs : None
matplotlib : 3.7.2
numba : None
numexpr : 2.8.4
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.10.1
sqlalchemy : None
tables : None
tabulate : 0.9.0
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None