Skip to content

REF: use _should_compare to short-circuit set operations #38643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
21 changes: 9 additions & 12 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down