Skip to content

Categorical.__setitem__ should allow setting a Categorical with same unordered categories #24142

Closed
@TomAugspurger

Description

@TomAugspurger

We'd need to recode the source categorical's codes to the target's.

In [6]: a = pd.Categorical(['a', 'b'], categories=['b', 'a'])

In [7]: b = pd.Categorical(['a', 'b'], categories=['a', 'b'])

In [8]: a[[True, False]] = b[[True, False]]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-8-9693ad7c05cc> in <module>
----> 1 a[[True, False]] = b[[True, False]]

~/sandbox/pandas/pandas/core/arrays/categorical.py in __setitem__(self, key, value)
   2082         if isinstance(value, Categorical):
   2083             if not value.categories.equals(self.categories):
-> 2084                 raise ValueError("Cannot set a Categorical with another, "
   2085                                  "without identical categories")
   2086

ValueError: Cannot set a Categorical with another, without identical categories

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions