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

Cannot replace all occurences of infs and nans to None with a single df.replace #31414

Open
dougvj opened this issue Jan 29, 2020 · 1 comment
Labels
Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate replace replace method

Comments

@dougvj
Copy link

dougvj commented Jan 29, 2020

Example code

>>> df = pd.DataFrame(
...         {'foo':[
...             np.nan,
...             np.inf,
...             1.1,
...             2.2
...             ],
...         'bar':[
...             1,
...             -np.inf,
...             2.1,
...             3.2
...             ]
...         })
>>> df
   foo  bar
0  NaN  1.0
1  inf -inf
2  1.1  2.1
3  2.2  3.2
>>> df.replace({np.nan: None, -np.inf:None, np.inf:None})
   foo  bar
0  NaN  1.0
1  NaN  NaN
2  1.1  2.1
3  2.2  3.2
>>> 

Problem description

When trying to replace all occurrences of NaNs and Infs to Nones the resulting dataframe simply contains 'NaNs' for every occurrence, retaining the dtype of float64 rather than the expected object. A work around is to simply issue the replace again with only np.nan to None.

This behavior was discovered when trying to convert the contents of a dataframe containing Infs, and NaNs to Nones in preparation for JSON compliant output via a dictionary for compatibility with third party tools rather than directly through panda's to_json() method (which admittedly would make this process unnecessary as it produces the compliant output already)

I strongly suspect this problem is related to inconsistencies outlined here: #29024

Expected Output

I would expect a column type of object with Nones in place as I see when replacing only NaNs.

Output of pd.show_versions()

[paste the output of pd.show_versions() here below this line]
INSTALLED VERSIONS

commit : None
python : 3.8.1.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.13-arch1-1
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.0.0rc0+233.gec0996c675
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 19.3
setuptools : 44.0.0
Cython : 0.29.14
pytest : None
hypothesis : None
sphinx : 2.2.1
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.4.2
html5lib : 1.0.1
pymysql : None
psycopg2 : None
jinja2 : 2.10.3
IPython : 7.11.1
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : 4.4.2
matplotlib : 3.1.2
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : 1.3.12
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

@s-scherrer
Copy link
Contributor

s-scherrer commented Jan 31, 2020

I can replicate this here:

>>> df = pd.DataFrame({
...         'foo':[np.nan, np.inf, 1.1, 2.2 ],
...         'bar':[ 1, -np.inf, 2.1, 3.2 ]
... })
>>> dfnew = df.replace({np.nan: None, -np.inf: None, np.inf: None})
>>> dfnew
   foo  bar
0  NaN  1.0
1  NaN  NaN
2  1.1  2.1
3  2.2  3.2

As @dougvj mentioned, running replace a second time works:

>>> dfnew.replace({np.nan: None, -np.inf: None, np.inf: None})
    foo   bar
0  None     1
1  None  None
2   1.1   2.1
3   2.2   3.2

However, if the replacement dictionary contains a replacement pair that will actually replace something, it doesn't work anymore:

>>> dfnew.replace({np.nan: None, 1: 2})
   foo  bar
0  NaN  2.0
1  NaN  NaN
2  1.1  2.1
>>>     

Replacing something else will also turn Nones back to Nans:

>>> dfnew.replace({np.nan: None, -np.inf: None, np.inf: None}).replace({1:2})
   foo  bar
0  NaN  2.0
1  NaN  NaN
2  1.1  2.1
3  2.2  3.2

So the issue here is probably that replace converts None to NaN.

@simonjayhawkins simonjayhawkins added Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate replace replace method labels Apr 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate replace replace method
Projects
None yet
Development

No branches or pull requests

3 participants