Skip to content

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

Closed
@jcrist

Description

@jcrist
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    CategoricalCategorical Data TypeDtype ConversionsUnexpected or buggy dtype conversions

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions