Skip to content

Commit 3cb2eec

Browse files
committed
REF: Added CategoricalIndex.union
The old behavior relied on `is_dtype_equal` to determine whether two iterables needed to be cast to `object`. With the change to our equality on `CategoricalIndex.dtype`, that check no longer provides what we want: "that two iterables can both be treated as categoricals".
1 parent 00ca906 commit 3cb2eec

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

pandas/core/indexes/category.py

+14
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,20 @@ def _evaluate_compare(self, other):
687687
cls.__le__ = _make_compare('__le__')
688688
cls.__ge__ = _make_compare('__ge__')
689689

690+
def union(self, other):
691+
"""
692+
Set union of a CategoricalIndex with some iterable
693+
"""
694+
from pandas.api.types import union_categoricals
695+
696+
if isinstance(other, CategoricalIndex):
697+
categories = union_categoricals([self, other]).categories
698+
left = self.set_categories(categories)
699+
right = other.set_categories(categories)
700+
else:
701+
left, right = self, other
702+
return super(CategoricalIndex, left).union(right)
703+
690704
def _delegate_method(self, name, *args, **kwargs):
691705
""" method delegation to the ._values """
692706
method = getattr(self._values, name)

pandas/tests/reshape/test_merge.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1481,7 +1481,7 @@ def test_dtype_on_merged_different(self, change, how, left, right):
14811481
X = change(right.X.astype('object'))
14821482
right = right.assign(X=X)
14831483
assert is_categorical_dtype(left.X.values)
1484-
assert not left.X.values.is_dtype_equal(right.X.values)
1484+
# assert not left.X.values.is_dtype_equal(right.X.values)
14851485

14861486
merged = pd.merge(left, right, on='X', how=how)
14871487

0 commit comments

Comments
 (0)