-
-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
Comments
You need to use
|
This actually isn't true in general. The replace works for [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'] |
The replace works for
Hence why I find it surprising it doesn't work for |
@MaximeLaurenty have put up a PR which restores this behavior, but also explains potential rationale for deprecating it and forcing use of |
hmm, we have the default of |
I agree I don't think fixing this regression is worth changing the default
would be the best. |
In 1.4.0 this is now the default behavior of replace, default value parameter is |
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
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
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
The text was updated successfully, but these errors were encountered: