BUG: DataFrame.to_records, DataFrame constructor broken for categoricals · Issue #8626 · pandas-dev/pandas (original) (raw)

to_records() and the df constructor are broken for data containing categoricals when created with dtype='category' or astype('category').

Broken (TypeError: data type not understood):

pd.DataFrame(list('abc'),dtype='category')

df=pd.DataFrame(list('abc'), columns=['A']) df['A'] = df['A'].astype('category') df.to_records()

Works:

pd.Series(list('abc'), dtype='category')

pd.DataFrame(list('abc'), dtype=pd.core.common.CategoricalDtype)

df=pd.DataFrame(list('abc'), columns=['A']) df['A'] = df['A'].astype(pd.core.common.CategoricalDtype) df.to_records()

pd.Series(list('abc'), dtype='category').to_frame().to_records()

to_frame seems to remove the category dtype though.

Pandas version: 0.15.0-20-g2737f5a