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

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

Closed
jcrist opened this Issue Dec 1, 2017 · 3 comments

Comments

Projects
None yet
3 participants
@jcrist
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

This comment has been minimized.

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 this to the Next Major Release milestone Dec 1, 2017

@jreback

This comment has been minimized.

Contributor

jreback commented Dec 1, 2017

@jorisvandenbossche

This comment has been minimized.

Member

jorisvandenbossche commented Dec 1, 2017

See also #10696

@jreback jreback modified the milestones: Next Major Release, 0.22.0 Dec 10, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment