Skip to content

Commit e50455f

Browse files
jbrockmendelluckyvs1
authored andcommitted
REF: use _should_compare to short-circuit set operations (pandas-dev#38643)
1 parent 38f92dd commit e50455f

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

pandas/core/indexes/base.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -2805,14 +2805,18 @@ def intersection(self, other, sort=False):
28052805
"""
28062806
self._validate_sort_keyword(sort)
28072807
self._assert_can_do_setop(other)
2808-
other, _ = self._convert_can_do_setop(other)
2808+
other, result_name = self._convert_can_do_setop(other)
28092809

28102810
if self.equals(other):
28112811
if self.has_duplicates:
28122812
return self.unique()._get_reconciled_name_object(other)
28132813
return self._get_reconciled_name_object(other)
28142814

2815-
if not is_dtype_equal(self.dtype, other.dtype):
2815+
elif not self._should_compare(other):
2816+
# We can infer that the intersection is empty.
2817+
return Index([], name=result_name)
2818+
2819+
elif not is_dtype_equal(self.dtype, other.dtype):
28162820
dtype = find_common_type([self.dtype, other.dtype])
28172821
this = self.astype(dtype, copy=False)
28182822
other = other.astype(dtype, copy=False)

pandas/core/indexes/datetimelike.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -683,13 +683,17 @@ def intersection(self, other, sort=False):
683683
"""
684684
self._validate_sort_keyword(sort)
685685
self._assert_can_do_setop(other)
686-
other, _ = self._convert_can_do_setop(other)
686+
other, result_name = self._convert_can_do_setop(other)
687687

688688
if self.equals(other):
689689
if self.has_duplicates:
690690
return self.unique()._get_reconciled_name_object(other)
691691
return self._get_reconciled_name_object(other)
692692

693+
elif not self._should_compare(other):
694+
# We can infer that the intersection is empty.
695+
return Index([], name=result_name)
696+
693697
return self._intersection(other, sort=sort)
694698

695699
def _intersection(self, other: Index, sort=False) -> Index:

pandas/core/indexes/period.py

+9-12
Original file line numberDiff line numberDiff line change
@@ -635,27 +635,24 @@ def _setop(self, other, sort, opname: str):
635635
def intersection(self, other, sort=False):
636636
self._validate_sort_keyword(sort)
637637
self._assert_can_do_setop(other)
638-
other, _ = self._convert_can_do_setop(other)
638+
other, result_name = self._convert_can_do_setop(other)
639639

640640
if self.equals(other):
641641
if self.has_duplicates:
642642
return self.unique()._get_reconciled_name_object(other)
643643
return self._get_reconciled_name_object(other)
644644

645-
return self._intersection(other, sort=sort)
646-
647-
def _intersection(self, other, sort=False):
645+
elif not self._should_compare(other):
646+
# We can infer that the intersection is empty.
647+
return Index([], name=result_name)
648648

649-
if is_object_dtype(other.dtype):
650-
return self.astype("O").intersection(other, sort=sort)
649+
elif not is_dtype_equal(self.dtype, other.dtype):
650+
# i.e. object dtype
651+
return super().intersection(other, sort=sort)
651652

652-
elif not self._is_comparable_dtype(other.dtype):
653-
# We can infer that the intersection is empty.
654-
# assert_can_do_setop ensures that this is not just a mismatched freq
655-
this = self[:0].astype("O")
656-
other = other[:0].astype("O")
657-
return this.intersection(other, sort=sort)
653+
return self._intersection(other, sort=sort)
658654

655+
def _intersection(self, other, sort=False):
659656
return self._setop(other, sort, opname="intersection")
660657

661658
def difference(self, other, sort=None):

0 commit comments

Comments
 (0)