diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 09fe885e47754..ad5de61cf665b 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -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) diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index bd92926941aa1..8af9d12e22ee0 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -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: @@ -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": """ diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 4aedf03ca1800..03f6387ae2dc5 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -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: