Skip to content

x.astype(categorical_dtype) ignored if x is already categorical #18593

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
jcrist opened this issue Dec 1, 2017 · 3 comments · Fixed by #18710
Closed

x.astype(categorical_dtype) ignored if x is already categorical #18593

jcrist opened this issue Dec 1, 2017 · 3 comments · Fixed by #18710
Labels
Categorical Categorical Data Type Dtype Conversions Unexpected or buggy dtype conversions
Milestone

Comments

@jcrist
Copy link
Contributor

jcrist commented Dec 1, 2017

In [1]: import pandas as pd

In [2]: a = pd.Series(['a', 'b', 'c'])

In [3]: dtype = pd.api.types.CategoricalDtype(['x', 'y', 'z'])

In [4]: b = a.astype('category')

In [5]: b
Out[5]:
0    a
1    b
2    c
dtype: category
Categories (3, object): [a, b, c]

In [6]: b.astype(dtype)
Out[6]:
0    a
1    b
2    c
dtype: category
Categories (3, object): [a, b, c]

In [7]: pd.__version__
Out[7]: '0.21.0'

I'd expect b.astype(dtype) to re-categorize b with the new categories from the categorical dtype. Instead, it seems this operation is ignored.

@jreback
Copy link
Contributor

jreback commented Dec 1, 2017

yeah this ignore it if its already categorical, agree should check if the cat is different and reconstruct a new one.

happy to take a PR

@jreback jreback added Categorical Categorical Data Type Difficulty Intermediate Dtype Conversions Unexpected or buggy dtype conversions labels Dec 1, 2017
@jreback jreback added this to the Next Major Release milestone Dec 1, 2017
@jreback
Copy link
Contributor

jreback commented Dec 1, 2017

cc @chris-b1

@jorisvandenbossche
Copy link
Member

See also #10696

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Categorical Categorical Data Type Dtype Conversions Unexpected or buggy dtype conversions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants