From 73b8e43a14371c4c24dee97d9de5367f58a1a53e Mon Sep 17 00:00:00 2001 From: Brock Date: Sun, 29 Nov 2020 10:39:40 -0800 Subject: [PATCH 1/3] follow-up edits --- pandas/core/indexes/base.py | 4 ++-- pandas/core/indexes/multi.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) 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/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: From b3ff4f975117dc17052f4508850786e77ff3b560 Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 30 Nov 2020 09:40:15 -0800 Subject: [PATCH 2/3] REF: IntervalIndex.intersection match pattern in other intersection methods --- pandas/core/indexes/interval.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index bd92926941aa1..ef34a0dcfeaea 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: Index, sort) -> Index: + """ + 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": """ From abcfaed5f88bb0f8fbec9567f5d60716716c45d3 Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 30 Nov 2020 10:19:52 -0800 Subject: [PATCH 3/3] mypy fixup --- pandas/core/indexes/interval.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index ef34a0dcfeaea..8af9d12e22ee0 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -976,7 +976,7 @@ def intersection(self, other, sort=False) -> Index: result = self._intersection(other, sort=sort) return self._wrap_setop_result(other, result) - def _intersection(self, other: Index, sort) -> Index: + def _intersection(self, other, sort): """ intersection specialized to the case with matching dtypes. """