diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index f5b9d0194833a..06cc995e7a8b9 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -2796,14 +2796,18 @@ def intersection(self, other, sort=False): """ self._validate_sort_keyword(sort) self._assert_can_do_setop(other) - other, _ = self._convert_can_do_setop(other) + other, result_name = self._convert_can_do_setop(other) if self.equals(other): if self.has_duplicates: return self.unique()._get_reconciled_name_object(other) return self._get_reconciled_name_object(other) - if not is_dtype_equal(self.dtype, other.dtype): + elif not self._should_compare(other): + # We can infer that the intersection is empty. + return Index([], name=result_name) + + elif not is_dtype_equal(self.dtype, other.dtype): dtype = find_common_type([self.dtype, other.dtype]) this = self.astype(dtype, copy=False) other = other.astype(dtype, copy=False) diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index 220cd5363e78f..7133411a26e67 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -683,13 +683,17 @@ def intersection(self, other, sort=False): """ self._validate_sort_keyword(sort) self._assert_can_do_setop(other) - other, _ = self._convert_can_do_setop(other) + other, result_name = self._convert_can_do_setop(other) if self.equals(other): if self.has_duplicates: return self.unique()._get_reconciled_name_object(other) return self._get_reconciled_name_object(other) + elif not self._should_compare(other): + # We can infer that the intersection is empty. + return Index([], name=result_name) + return self._intersection(other, sort=sort) def _intersection(self, other: Index, sort=False) -> Index: diff --git a/pandas/core/indexes/period.py b/pandas/core/indexes/period.py index f8a62c6a8e006..d5822d919ae64 100644 --- a/pandas/core/indexes/period.py +++ b/pandas/core/indexes/period.py @@ -635,27 +635,24 @@ def _setop(self, other, sort, opname: str): def intersection(self, other, sort=False): self._validate_sort_keyword(sort) self._assert_can_do_setop(other) - other, _ = self._convert_can_do_setop(other) + other, result_name = self._convert_can_do_setop(other) if self.equals(other): if self.has_duplicates: return self.unique()._get_reconciled_name_object(other) return self._get_reconciled_name_object(other) - return self._intersection(other, sort=sort) - - def _intersection(self, other, sort=False): + elif not self._should_compare(other): + # We can infer that the intersection is empty. + return Index([], name=result_name) - if is_object_dtype(other.dtype): - return self.astype("O").intersection(other, sort=sort) + elif not is_dtype_equal(self.dtype, other.dtype): + # i.e. object dtype + return super().intersection(other, sort=sort) - elif not self._is_comparable_dtype(other.dtype): - # We can infer that the intersection is empty. - # assert_can_do_setop ensures that this is not just a mismatched freq - this = self[:0].astype("O") - other = other[:0].astype("O") - return this.intersection(other, sort=sort) + return self._intersection(other, sort=sort) + def _intersection(self, other, sort=False): return self._setop(other, sort, opname="intersection") def difference(self, other, sort=None):