Skip to content

REF: de-duplicate symmetric_difference, _union #41833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 6 additions & 26 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3246,33 +3246,13 @@ def symmetric_difference(self, other, result_name=None, sort=None):
if result_name is None:
result_name = result_name_update

if not self._should_compare(other):
return self.union(other, sort=sort).rename(result_name)
elif not is_dtype_equal(self.dtype, other.dtype):
dtype = find_common_type([self.dtype, other.dtype])
this = self.astype(dtype, copy=False)
that = other.astype(dtype, copy=False)
return this.symmetric_difference(that, sort=sort).rename(result_name)

this = self._get_unique_index()
other = other._get_unique_index()
indexer = this.get_indexer_for(other)
left = self.difference(other, sort=False)
right = other.difference(self, sort=False)
result = left.union(right, sort=sort)

# {this} minus {other}
common_indexer = indexer.take((indexer != -1).nonzero()[0])
left_indexer = np.setdiff1d(
np.arange(this.size), common_indexer, assume_unique=True
)
left_diff = this._values.take(left_indexer)

# {other} minus {this}
right_indexer = (indexer == -1).nonzero()[0]
right_diff = other._values.take(right_indexer)

the_diff = concat_compat([left_diff, right_diff])
the_diff = _maybe_try_sort(the_diff, sort)

return Index(the_diff, name=result_name)
if result_name is not None:
result = result.rename(result_name)
return result

@final
def _assert_can_do_setop(self, other) -> bool:
Expand Down
4 changes: 4 additions & 0 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ def _is_dtype_compat(self, other) -> Categorical:
raise TypeError(
"categories must match existing categories when appending"
)

elif other._is_multi:
# preempt raising NotImplementedError in isna call
raise TypeError("MultiIndex is not dtype-compatible with CategoricalIndex")
else:
values = other

Expand Down
7 changes: 1 addition & 6 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
inherit_names,
make_wrapped_arith_op,
)
from pandas.core.indexes.numeric import Int64Index
from pandas.core.tools.timedeltas import to_timedelta

if TYPE_CHECKING:
Expand Down Expand Up @@ -779,11 +778,7 @@ def _union(self, other, sort):
# that result.freq == self.freq
return result
else:
i8self = Int64Index._simple_new(self.asi8)
i8other = Int64Index._simple_new(other.asi8)
i8result = i8self._union(i8other, sort=sort)
result = type(self)(i8result, dtype=self.dtype, freq="infer")
return result
return super()._union(other, sort=sort)._with_freq("infer")

# --------------------------------------------------------------------
# Join Methods
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3680,9 +3680,9 @@ def symmetric_difference(self, other, result_name=None, sort=None):
return type(self)(
levels=[[] for _ in range(self.nlevels)],
codes=[[] for _ in range(self.nlevels)],
names=tups.name,
names=tups.names,
)
return type(self).from_tuples(tups, names=tups.name)
return tups

# --------------------------------------------------------------------

Expand Down
12 changes: 0 additions & 12 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,18 +730,6 @@ def _difference(self, other, sort=None):
new_index = new_index[::-1]
return new_index

def symmetric_difference(self, other, result_name: Hashable = None, sort=None):
if not isinstance(other, RangeIndex) or sort is not None:
return super().symmetric_difference(other, result_name, sort)

left = self.difference(other)
right = other.difference(self)
result = left.union(right)

if result_name is not None:
result = result.rename(result_name)
return result

# --------------------------------------------------------------------

def _concat(self, indexes: list[Index], name: Hashable) -> Index:
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/indexes/categorical/test_equals.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Categorical,
CategoricalIndex,
Index,
MultiIndex,
)


Expand Down Expand Up @@ -79,3 +80,11 @@ def test_equals_non_category(self):
other = Index(["A", "B", "D", np.nan])

assert not ci.equals(other)

def test_equals_multiindex(self):
# dont raise NotImplementedError when calling is_dtype_compat

mi = MultiIndex.from_arrays([["A", "B", "C", "D"], range(4)])
ci = mi.to_flat_index().astype("category")

assert not ci.equals(mi)