Skip to content

BUG: series.replace(np.nan,..) on categorical series does not replace #40472

Open
@MaximeLaurenty

Description

@MaximeLaurenty
  • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    CategoricalCategorical Data TypeDeprecateFunctionality to remove in pandasNeeds DiscussionRequires discussion from core team before further actionRegressionFunctionality that used to work in a prior pandas versionreplacereplace method

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions