Skip to content

Series.replace fails on categoricals with list #31720

Closed
@bnaul

Description

@bnaul

Looks like this was introduced in #27026: not sure of the details of what was incorrect in the old version but I do know that this at least didn't error:

cat = pd.CategoricalIndex(['a', 'b']).to_series()
cat.replace('a', 'A')  # works
cat.replace(['a'], 'A')
~/model/.venv/lib/python3.7/site-packages/pandas/core/arrays/categorical.py in replace(self, to_replace, value, inplace)
   2440         inplace = validate_bool_kwarg(inplace, "inplace")
   2441         cat = self if inplace else self.copy()
-> 2442         if to_replace in cat.categories:
   2443             if isna(value):
   2444                 cat.remove_categories(to_replace, inplace=True)

~/model/.venv/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'

Seems like most of the methods used in this implementation already handle list-like inputs so it should be a pretty easy fix..?

Output of pd.show_versions()

pandas : 1.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    CategoricalCategorical Data TypeRegressionFunctionality that used to work in a prior pandas version

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions