Skip to content
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

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

Closed
TomAugspurger opened this issue Dec 7, 2018 · 2 comments
Labels
Categorical Categorical Data Type
Milestone

Comments

@TomAugspurger
Copy link
Contributor

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
@TomAugspurger TomAugspurger added the Categorical Categorical Data Type label Dec 7, 2018
@TomAugspurger
Copy link
Contributor Author

I may handle this in
#24114, since it's blocking a setitem-based where.

@jorisvandenbossche
Copy link
Member

And does the where operation need to work with unequal categorical dtypes?

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

No branches or pull requests

3 participants