ENH: union_categorical supports identical categories with ordered by sinhrks · Pull Request #13763 · pandas-dev/pandas (original) (raw)

.append / concat can handle ordered Categorical which has the identical categories. However, union_categorical raises TypeError. This PR allows union_categorical to handle the case.

s1 = pd.Series(pd.Categorical([1, 2, 3], ordered=True))
s2 = pd.Series(pd.Categorical([3, 2, 1], categories=[1, 2, 3], ordered=True))
s1.append(s2)

#0    1
#1    2
#2    3
#0    3
#1    2
#2    1
# dtype: category
# Categories (3, int64): [1 < 2 < 3]

pd.types.concat.union_categoricals([s1.values, s2.values])
# TypeError: Can only combine unordered Categoricals