Skip to content

[Regression] DataFrame.replace throws TypeError when to_replace is list and DF has categorical #32497

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

Closed
buhrmann opened this issue Mar 6, 2020 · 3 comments

Comments

@buhrmann
Copy link

buhrmann commented Mar 6, 2020

Code Sample

df = pd.DataFrame({
    'x': pd.Series([-np.inf, np.inf]),
    'y': pd.Series(["a", "b"], dtype="category")
})

df.replace([-np.inf, np.inf], np.nan)

results in

...
~/anaconda/envs/grapy/lib/python3.7/site-packages/pandas/core/indexes/base.py in __contains__(self, key)
   3898     @Appender(_index_shared_docs["contains"] % _index_doc_kwargs)
   3899     def __contains__(self, key) -> bool:
-> 3900         hash(key)
   3901         try:
   3902             return key in self._engine

TypeError: unhashable type: 'list'

Problem description

When a DF contains a categorical column, and when to_replace argument is a list, replace() throws TypeError: unhashable type: 'list'.

Expected Output

When the to_replace argument is a tuple rather than a list (which is not a documented option), all seems good:

df.replace((-np.inf, np.inf), np.nan)
    x  y
0 NaN  a
1 NaN  b

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.7.6.final.0
python-bits : 64
OS : Darwin
OS-release : 19.2.0
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8

pandas : 1.0.1
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.0
pip : 20.0.2
setuptools : 45.2.0.post20200209
Cython : None
pytest : 5.3.5
hypothesis : None
sphinx : 2.4.3
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.4.1
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.1
IPython : 7.12.0
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : 4.4.1
matplotlib : 3.1.3
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 0.16.0
pytables : None
pytest : 5.3.5
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : None
xlsxwriter : None
numba : 0.48.0

@buhrmann buhrmann changed the title DataFrame.replace throws TypeError when to_replace is list and DF has categorical [Regression] DataFrame.replace throws TypeError when to_replace is list and DF has categorical Mar 6, 2020
@buhrmann
Copy link
Author

buhrmann commented Mar 6, 2020

Forgot to mention, this is a regression and used to work in 0.25.3.

@TomAugspurger
Copy link
Contributor

Thanks for the report. Duplicate of #31720 and fixed in #31734.

@buhrmann
Copy link
Author

buhrmann commented Mar 7, 2020

Ok, thanks. Sorry for creating noise, but I didn't see the duplicate among related issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants