In answering this question, I found that after using melt
on a pandas dataframe, a column that was previously an ordered Categorical dtype becomes an object
. Is this intended behaviour?
Note: not looking for a solution, just wondering if there is any reason for this behaviour or if it's not intended behavior.
Example:
Using the following dataframe df
:
Cat L_1 L_2 L_3
0 A 1 2 3
1 B 4 5 6
2 C 7 8 9df['Cat'] = pd.Categorical(df['Cat'], categories = ['C','A','B'], ordered=True)# As you can see `Cat` is a category
>>> df.dtypes
Cat category
L_1 int64
L_2 int64
L_3 int64
dtype: objectmelted = df.melt('Cat')>>> meltedCat variable value
0 A L_1 1
1 B L_1 4
2 C L_1 7
3 A L_2 2
4 B L_2 5
5 C L_2 8
6 A L_3 3
7 B L_3 6
8 C L_3 9
Now, if I look at Cat
, it's become an object:
>>> melted.dtypes
Cat object
variable object
value int64
dtype: object
Is this intended?