x.astype(categorical_dtype) ignored if x is already categorical · Issue #18593 · pandas-dev/pandas (original) (raw)

Skip to content

Provide feedback

Saved searches

Use saved searches to filter your results more quickly

Sign up

Appearance settings

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