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: series.replace(np.nan,..) on categorical series does not replace #40472

Open
2 of 3 tasks
MaximeLaurenty opened this issue Mar 16, 2021 · 7 comments
Open
2 of 3 tasks
Labels
Categorical Categorical Data Type Deprecate Functionality to remove in pandas Needs Discussion Requires discussion from core team before further action Regression Functionality that used to work in a prior pandas version replace replace method

Comments

@MaximeLaurenty
Copy link

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

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

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

import pandas as pd
import numpy as np  
#good
good = pd.Series({1: 'a', 2: 'b'}).astype('category').replace('a', 'c')
#bad
bad = pd.Series({1: np.nan, 2: 'b'}).astype('category').replace(np.nan, 'c')
# does not replace and bad is instead:
# 1    NaN
# 2      b
# dtype: category
# Categories (1, object): [b]

Problem description

When replacing np.nan on a categorical series, the values are not modified.
This is a breaking change introduced in 1.0 (it worked fine in 0.25.3).

My guess is that this was introduced by https://github.com/pandas-dev/pandas/pull/27026/files
which does nothing when "to_replace in cat.categories" evaluates to False.

Expected Output

pd.Series({1: 'c', 2: 'b'}).astype('category')
displaying like

# 1      c
# 2      b
# dtype: category
# Categories (2, object): [c, b]

Output of pd.show_versions()

INSTALLED VERSIONS

commit : f2c8480
python : 3.8.8.final.0
python-bits : 64
OS : Darwin
OS-release : 19.6.0
Version : Darwin Kernel Version 19.6.0: Thu Oct 29 22:56:45 PDT 2020; root:xnu-6153.141.2.2~1/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8

pandas : 1.2.3
numpy : 1.19.2
pytz : 2021.1
dateutil : 2.8.1
pip : 21.0.1
setuptools : 52.0.0.post20210125
Cython : 0.29.22
pytest : 4.5.0
hypothesis : None
sphinx : 3.5.2
blosc : None
feather : None
xlsxwriter : 1.3.7
lxml.etree : 4.6.2
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.3
IPython : 7.21.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : 1.3.2
fsspec : 0.8.3
fastparquet : None
gcsfs : None
matplotlib : 3.3.4
numexpr : 2.7.3
odfpy : None
openpyxl : 3.0.7
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.6.1
sqlalchemy : 1.3.23
tables : 3.6.1
tabulate : None
xarray : None
xlrd : 2.0.1
xlwt : 1.3.0
numba : 0.53.0

@MaximeLaurenty MaximeLaurenty added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 16, 2021
@dsaxton dsaxton added Categorical Categorical Data Type Regression Functionality that used to work in a prior pandas version replace replace method and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 16, 2021
@jenhseb
Copy link

jenhseb commented Mar 17, 2021

You need to use fillna for NaN values. Notice that np.nan == np.nan returns False. Thus, replace isn't able to match it.

import pandas as pd
import numpy as np  

pd.Series({1: np.nan, 2: 'b'}).fillna('c')

@dsaxton
Copy link
Member

dsaxton commented Mar 17, 2021

You need to use fillna for NaN values. Notice that np.nan == np.nan returns False. Thus, replace isn't able to match it.

import pandas as pd
import numpy as np  

pd.Series({1: np.nan, 2: 'b'}).fillna('c')

This actually isn't true in general. The replace works for object but not category:

[ins] In [8]: pd.Series([np.nan, "a"]).replace(np.nan, "a")
Out[8]: 
0    a
1    a
dtype: object

[ins] In [9]: pd.Series([np.nan, "a"], dtype="category").replace(np.nan, "a")
Out[9]: 
0    NaN
1      a
dtype: category
Categories (1, object): ['a']

@MaximeLaurenty
Copy link
Author

The replace works for object & float :

In [7]: pd.Series([np.nan, 2]).replace(np.nan, 1)
Out[7]: 
0    1.0
1    2.0
dtype: float64

Hence why I find it surprising it doesn't work for category anymore.

@mzeitlin11
Copy link
Member

@MaximeLaurenty have put up a PR which restores this behavior, but also explains potential rationale for deprecating it and forcing use of fillna. Please let me know if you have any thoughts on what makes more sense!

@jreback
Copy link
Contributor

jreback commented Mar 29, 2021

hmm, we have the default of replace=None so accepting np.nan here is a bit odd. we could change this to use no_default and then this might be reasonable.

@MaximeLaurenty
Copy link
Author

I agree fillna is better (and I changed our code to use it right after after spotting our issue).

I don't think fixing this regression is worth changing the default to_replace.
In my opinion having:

  • a warning that it'll be deprecated like suggested by @mzeitlin11
  • and a fix until then that doesn't interfere with None

would be the best.
(But I've no experience in maintaining open-source libraries, hence it's not a strong opinion)

@mzeitlin11 mzeitlin11 removed their assignment Apr 8, 2021
simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Jun 7, 2021
@mroeschke mroeschke added Deprecate Functionality to remove in pandas Needs Discussion Requires discussion from core team before further action labels Aug 19, 2021
@roib20
Copy link
Contributor

roib20 commented Jan 30, 2022

hmm, we have the default of replace=None so accepting np.nan here is a bit odd. we could change this to use no_default and then this might be reasonable.

In 1.4.0 this is now the default behavior of replace, default value parameter is value: NoDefault = lib.no_default. But this specific bug is still present from my testing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Categorical Categorical Data Type Deprecate Functionality to remove in pandas Needs Discussion Requires discussion from core team before further action Regression Functionality that used to work in a prior pandas version replace replace method
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants