Skip to content

CLN: setops #42228

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 1 commit into from
Jun 25, 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
21 changes: 18 additions & 3 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3089,8 +3089,6 @@ def _intersection(self, other: Index, sort=False):
"""
intersection specialized to the case with matching dtypes.
"""
# TODO(EA): setops-refactor, clean all this up

if self.is_monotonic and other.is_monotonic:
try:
result = self._inner_indexer(other)[0]
Expand All @@ -3109,6 +3107,7 @@ def _wrap_intersection_result(self, other, result):
# We will override for MultiIndex to handle empty results
return self._wrap_setop_result(other, result)

@final
def _intersection_via_get_indexer(self, other: Index, sort) -> ArrayLike:
"""
Find the intersection of two Indexes using get_indexer.
Expand Down Expand Up @@ -3184,9 +3183,10 @@ def difference(self, other, sort=None):
return self.rename(result_name)

result = self._difference(other, sort=sort)
return self._wrap_setop_result(other, result)
return self._wrap_difference_result(other, result)

def _difference(self, other, sort):
# overridden by RangeIndex

this = self.unique()

Expand All @@ -3199,6 +3199,11 @@ def _difference(self, other, sort):

return the_diff

def _wrap_difference_result(self, other, result):
# We will override for MultiIndex to handle empty results
return self._wrap_setop_result(other, result)

@final
def symmetric_difference(self, other, result_name=None, sort=None):
"""
Compute the symmetric difference of two Index objects.
Expand Down Expand Up @@ -3246,6 +3251,16 @@ def symmetric_difference(self, other, result_name=None, sort=None):

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

if self._is_multi and len(result) == 0:
# On equal symmetric_difference MultiIndexes the difference is empty.
# Therefore, an empty MultiIndex is returned GH#13490
return type(self)(
levels=[[] for _ in range(self.nlevels)],
codes=[[] for _ in range(self.nlevels)],
names=result.names,
)

return result

@final
Expand Down
24 changes: 5 additions & 19 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3552,7 +3552,7 @@ def _maybe_match_names(self, other):
return names

def _wrap_intersection_result(self, other, result):
other, result_names = self._convert_can_do_setop(other)
_, result_names = self._convert_can_do_setop(other)

if len(result) == 0:
return MultiIndex(
Expand All @@ -3564,20 +3564,18 @@ def _wrap_intersection_result(self, other, result):
else:
return MultiIndex.from_arrays(zip(*result), sortorder=0, names=result_names)

def _difference(self, other, sort) -> MultiIndex:
other, result_names = self._convert_can_do_setop(other)

difference = super()._difference(other, sort)
def _wrap_difference_result(self, other, result):
_, result_names = self._convert_can_do_setop(other)

if len(difference) == 0:
if len(result) == 0:
return MultiIndex(
levels=[[]] * self.nlevels,
codes=[[]] * self.nlevels,
names=result_names,
verify_integrity=False,
)
else:
return MultiIndex.from_tuples(difference, sortorder=0, names=result_names)
return MultiIndex.from_tuples(result, sortorder=0, names=result_names)

def _convert_can_do_setop(self, other):
result_names = self.names
Expand All @@ -3599,18 +3597,6 @@ def _convert_can_do_setop(self, other):

return other, result_names

def symmetric_difference(self, other, result_name=None, sort=None):
# On equal symmetric_difference MultiIndexes the difference is empty.
# Therefore, an empty MultiIndex is returned GH13490
tups = Index.symmetric_difference(self, other, result_name, sort)
if len(tups) == 0:
return type(self)(
levels=[[] for _ in range(self.nlevels)],
codes=[[] for _ in range(self.nlevels)],
names=tups.names,
)
return tups

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

@doc(Index.astype)
Expand Down