Skip to content

REF: IntervalIndex._assert_can_do_setop #38112

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
Nov 28, 2020
Merged
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
31 changes: 20 additions & 11 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def setop_check(method):

@wraps(method)
def wrapped(self, other, sort=False):
self._validate_sort_keyword(sort)
self._assert_can_do_setop(other)
other = ensure_index(other)

Expand All @@ -131,14 +132,6 @@ def wrapped(self, other, sort=False):
result = result.astype(self.dtype)
return result

if self._is_non_comparable_own_type(other):
# GH#19016: ensure set op will not return a prohibited dtype
raise TypeError(
"can only do set operations between two IntervalIndex "
"objects that are closed on the same side "
"and have compatible dtypes"
)

return method(self, other, sort)

return wrapped
Expand Down Expand Up @@ -956,11 +949,27 @@ def _format_space(self) -> str:
# --------------------------------------------------------------------
# Set Operations

def _assert_can_do_setop(self, other):
super()._assert_can_do_setop(other)

if isinstance(other, IntervalIndex) and self._is_non_comparable_own_type(other):
# GH#19016: ensure set op will not return a prohibited dtype
raise TypeError(
"can only do set operations between two IntervalIndex "
"objects that are closed on the same side "
"and have compatible dtypes"
)

@Appender(Index.intersection.__doc__)
@setop_check
def intersection(
self, other: "IntervalIndex", sort: bool = False
) -> "IntervalIndex":
def intersection(self, other, sort=False) -> Index:
self._validate_sort_keyword(sort)
self._assert_can_do_setop(other)
other, _ = self._convert_can_do_setop(other)

if not isinstance(other, IntervalIndex):
return self.astype(object).intersection(other)

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:
Expand Down