Closed
Description
Code Sample, a copy-pastable example if possible
idx = pd.MultiIndex.from_product([['A'], [0, 1]])
df = pd.DataFrame({'cat': pd.Categorical(['a', 'b'])}, index=idx)
df
Out[19]:
cat
A 0 a
1 b
df.unstack()
Out[21]:
cat
0 1
A a b
Categorical dtype is lost:
df.unstack().dtypes
Out[22]:
cat 0 object
1 object
dtype: object
But it works ok for a Series:
df['cat'].unstack()
Out[24]:
0 1
A a b
df['cat'].unstack().dtypes
Out[25]:
0 category
1 category
dtype: object
Expected Output
I believe df.unstack()
should preserve categorical dtypes as df['cat'].unstack()
does.
output of pd.show_versions()
pd.show_versions()
INSTALLED VERSIONS
------------------
commit: 5d791cc7d955c0b074ad602eb03fa32bd3e17503
python: 3.5.1.final.0
python-bits: 64
OS: Linux
OS-release: 4.1.20-1
machine: x86_64
processor: Intel(R)_Core(TM)_i5-2520M_CPU_@_2.50GHz
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
pandas: 0.18.1+368.g5d791cc
nose: 1.3.7
pip: 8.1.2
setuptools: 21.0.0
Cython: 0.24
numpy: 1.11.0
...
This is related to #13743 (and PR #13854), though a bit more difficult to fix.