Skip to content

ENH: Support For Interval __contains__ Other Interval (#46613) #47927

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 12 commits into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
ENH: Support For Interval __contains__ Other Interval (#46613)
  • Loading branch information
kapiliyer authored and hdrodz committed Aug 5, 2022
commit 29336eb42f5fbf1fa4fe6633fe8ed14662b3e638
8 changes: 5 additions & 3 deletions pandas/_libs/interval.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,11 @@ cdef class Interval(IntervalMixin):
return hash((self.left, self.right, self.inclusive))

def __contains__(self, key) -> bool:
if isinstance(key, Interval):
return ((self.left < key.left if self.open_left and key.closed_left else self.left <= key.left) and
(key.right < self.right if self.open_right and key.closed_right else key.right <= self.right))
if _interval_like(key):
key_closed_left = key.inclusive in ('left', 'both')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @venaturum if you'd like to review if this aligns with piso

key_closed_right = key.inclusive in ('right', 'both')
return ((self.left < key.left if self.open_left and key_closed_left else self.left <= key.left) and
(key.right < self.right if self.open_right and key_closed_right else key.right <= self.right))
return ((self.left < key if self.open_left else self.left <= key) and
(key < self.right if self.open_right else key <= self.right))

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def __new__(

# validate the arguments
if com.all_none(start, stop, step):
raise TypeError("RangeIndex(...) must be called with integers")
raise TypeError("RangeIndex(...) must be called with integers/floats")

start = ensure_python_int(start) if start is not None else 0

Expand Down