-
[X] I have checked that this issue has not already been reported.
-
[X] I have confirmed this bug exists on the latest version of pandas.
-
[X] (optional) I have confirmed this bug exists on the master branch of pandas.
Coming from here, I tried my issue on current HEAD@master and the erroneous behaviour still persists.
Code Sample, a copy-pastable example
import pandas as pd
cat = pd.CategoricalDtype(categories=[1, 2, 3, 4])
df = pd.DataFrame({'a': [1, 2, 3, 4, 3, 2, 1], 'b': [4, 3, 2, 3, 1, 2, 3]}, dtype=cat)
# flat categorical index, no MultiIndex
df_cat = df.set_index('a', drop=False)
diff_ind = df_cat.index.difference(df_cat.head(3).index)
print(diff_ind.dtype) # -> category
union_ind = df_cat.index.union(df_cat.head(3).index)
print(union_ind.dtype) # -> category
# same set operations on MultiIndex, categorical gets lost
df_cat_multi = df.set_index(['a', 'b'], drop=False)
# just make sure, we have a categorical in the first place
print(df_cat_multi.index.get_level_values(0).dtype) # -> category
print(df_cat_multi.index.get_level_values(1).dtype) # -> category
diff_multi_ind = df_cat_multi.index.difference(df_cat_multi.head(3).index)
print(diff_multi_ind.get_level_values(0).dtype) # -> int64
print(diff_multi_ind.get_level_values(1).dtype) # -> int64
union_multi_ind = df_cat_multi.index.union(df_cat_multi.head(3).index)
print(union_multi_ind.get_level_values(0).dtype) # -> int64
print(union_multi_ind.get_level_values(1).dtype) # -> int64
Problem description
In contrast to set operations involving flat categorical indices, the same operations on multi-indices with categorical level values revert the result back to the underlying datatype.
Expected Output
The set-operations on multi-indices should preserve the categorical dtype of level values.
Output of pd.show_versions()
Comment From: mzeitlin11
Thanks for the detailed report, @tscheburaschka! Investigations to fix are certainly welcome!
Comment From: lukemanley
This appears to be fixed on main. Could use a test.