-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: Implement IntervalIndex.is_overlapping #23327
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,6 +104,7 @@ def _new_IntervalIndex(cls, d): | |
summary="Immutable index of intervals that are closed on the same side.", | ||
name=_index_doc_kwargs['name'], | ||
versionadded="0.20.0", | ||
extra_attributes="is_overlapping\n", | ||
extra_methods="contains\n", | ||
examples=textwrap.dedent("""\ | ||
Examples | ||
|
@@ -464,6 +465,61 @@ def is_unique(self): | |
def is_non_overlapping_monotonic(self): | ||
return self._data.is_non_overlapping_monotonic | ||
|
||
@property | ||
def is_overlapping(self): | ||
""" | ||
Return True if the IntervalIndex has overlapping intervals, else False. | ||
|
||
Two intervals overlap if they share a common point, including closed | ||
endpoints. Intervals that only have an open endpoint in common do not | ||
overlap. | ||
|
||
.. versionadded:: 0.24.0 | ||
|
||
Returns | ||
------- | ||
bool | ||
Boolean indicating if the IntervalIndex has overlapping intervals. | ||
|
||
Examples | ||
-------- | ||
>>> index = pd.IntervalIndex.from_tuples([(0, 2), (1, 3), (4, 5)]) | ||
>>> index | ||
IntervalIndex([(0, 2], (1, 3], (4, 5]], | ||
closed='right', | ||
dtype='interval[int64]') | ||
>>> index.is_overlapping | ||
True | ||
|
||
Intervals that share closed endpoints overlap: | ||
|
||
>>> index = pd.interval_range(0, 3, closed='both') | ||
>>> index | ||
IntervalIndex([[0, 1], [1, 2], [2, 3]], | ||
closed='both', | ||
dtype='interval[int64]') | ||
>>> index.is_overlapping | ||
True | ||
|
||
Intervals that only have an open endpoint in common do not overlap: | ||
|
||
>>> index = pd.interval_range(0, 3, closed='left') | ||
>>> index | ||
IntervalIndex([[0, 1), [1, 2), [2, 3)], | ||
closed='left', | ||
dtype='interval[int64]') | ||
>>> index.is_overlapping | ||
False | ||
|
||
See Also | ||
-------- | ||
Interval.overlaps : Check whether two Interval objects overlap. | ||
IntervalIndex.overlaps : Check an IntervalIndex elementwise for | ||
overlaps. | ||
""" | ||
# GH 23309 | ||
return self._engine.is_overlapping | ||
|
||
@Appender(_index_shared_docs['_convert_scalar_indexer']) | ||
def _convert_scalar_indexer(self, key, kind=None): | ||
if kind == 'iloc': | ||
|
@@ -570,6 +626,10 @@ def _maybe_convert_i8(self, key): | |
else: | ||
# DatetimeIndex/TimedeltaIndex | ||
key_dtype, key_i8 = key.dtype, Index(key.asi8) | ||
if key.hasnans: | ||
# convert NaT from it's i8 value to np.nan so it's not viewed | ||
# as a valid value, maybe causing errors (e.g. is_overlapping) | ||
key_i8 = key_i8.where(~key._isnan) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Specifically, without this change a datetime-like Since I've added a relevant |
||
|
||
# ensure consistency with IntervalIndex subtype | ||
subtype = self.dtype.subtype | ||
|
Uh oh!
There was an error while loading. Please reload this page.