Skip to content

REF: IntervalIndex.intersection match pattern in other intersection methods #38190

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 4 commits into from
Dec 2, 2020
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
4 changes: 2 additions & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2828,8 +2828,8 @@ def intersection(self, other, sort=False):

if not is_dtype_equal(self.dtype, other.dtype):
dtype = find_common_type([self.dtype, other.dtype])
this = self.astype(dtype)
other = other.astype(dtype)
this = self.astype(dtype, copy=False)
other = other.astype(dtype, copy=False)
return this.intersection(other, sort=sort)

result = self._intersection(other, sort=sort)
Expand Down
13 changes: 12 additions & 1 deletion pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -967,9 +967,20 @@ def intersection(self, other, sort=False) -> Index:
self._assert_can_do_setop(other)
other, _ = self._convert_can_do_setop(other)

if self.equals(other) and not self.has_duplicates:
return self._get_reconciled_name_object(other)

if not isinstance(other, IntervalIndex):
return self.astype(object).intersection(other)

result = self._intersection(other, sort=sort)
return self._wrap_setop_result(other, result)

def _intersection(self, other, sort):
"""
intersection specialized to the case with matching dtypes.
"""
# For IntervalIndex we also know other.closed == self.closed
if self.left.is_unique and self.right.is_unique:
taken = self._intersection_unique(other)
elif other.left.is_unique and other.right.is_unique and self.isna().sum() <= 1:
Expand All @@ -983,7 +994,7 @@ def intersection(self, other, sort=False) -> Index:
if sort is None:
taken = taken.sort_values()

return self._wrap_setop_result(other, taken)
return taken

def _intersection_unique(self, other: "IntervalIndex") -> "IntervalIndex":
"""
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3723,7 +3723,7 @@ def _convert_can_do_setop(self, other):
try:
other = MultiIndex.from_tuples(other)
except (ValueError, TypeError) as err:
# ValueError raised by tupels_to_object_array if we
# ValueError raised by tuples_to_object_array if we
# have non-object dtype
raise TypeError(msg) from err
else:
Expand Down