Skip to content

Commit aed5dba

Browse files
authored
REF: IntervalIndex.intersection match pattern in other intersection methods (#38190)
1 parent b36d539 commit aed5dba

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

pandas/core/indexes/base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2828,8 +2828,8 @@ def intersection(self, other, sort=False):
28282828

28292829
if not is_dtype_equal(self.dtype, other.dtype):
28302830
dtype = find_common_type([self.dtype, other.dtype])
2831-
this = self.astype(dtype)
2832-
other = other.astype(dtype)
2831+
this = self.astype(dtype, copy=False)
2832+
other = other.astype(dtype, copy=False)
28332833
return this.intersection(other, sort=sort)
28342834

28352835
result = self._intersection(other, sort=sort)

pandas/core/indexes/interval.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -967,9 +967,20 @@ def intersection(self, other, sort=False) -> Index:
967967
self._assert_can_do_setop(other)
968968
other, _ = self._convert_can_do_setop(other)
969969

970+
if self.equals(other) and not self.has_duplicates:
971+
return self._get_reconciled_name_object(other)
972+
970973
if not isinstance(other, IntervalIndex):
971974
return self.astype(object).intersection(other)
972975

976+
result = self._intersection(other, sort=sort)
977+
return self._wrap_setop_result(other, result)
978+
979+
def _intersection(self, other, sort):
980+
"""
981+
intersection specialized to the case with matching dtypes.
982+
"""
983+
# For IntervalIndex we also know other.closed == self.closed
973984
if self.left.is_unique and self.right.is_unique:
974985
taken = self._intersection_unique(other)
975986
elif other.left.is_unique and other.right.is_unique and self.isna().sum() <= 1:
@@ -983,7 +994,7 @@ def intersection(self, other, sort=False) -> Index:
983994
if sort is None:
984995
taken = taken.sort_values()
985996

986-
return self._wrap_setop_result(other, taken)
997+
return taken
987998

988999
def _intersection_unique(self, other: "IntervalIndex") -> "IntervalIndex":
9891000
"""

pandas/core/indexes/multi.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3723,7 +3723,7 @@ def _convert_can_do_setop(self, other):
37233723
try:
37243724
other = MultiIndex.from_tuples(other)
37253725
except (ValueError, TypeError) as err:
3726-
# ValueError raised by tupels_to_object_array if we
3726+
# ValueError raised by tuples_to_object_array if we
37273727
# have non-object dtype
37283728
raise TypeError(msg) from err
37293729
else:

0 commit comments

Comments
 (0)