Skip to content
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

BUG: IntervalIndex is_non_overlapping_monotonic is incorrect when endpoints are closed on both sides #16588

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
.ipynb_checkpoints
.tags
.cache/
.vscode

# Compiled source #
###################
Expand Down
8 changes: 6 additions & 2 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,8 +556,12 @@ def is_non_overlapping_monotonic(self):
# must be increasing (e.g., [0, 1), [1, 2), [2, 3), ... )
# or decreasing (e.g., [-1, 0), [-2, -1), [-3, -2), ...)
# we already require left <= right
return ((self.right[:-1] <= self.left[1:]).all() or
(self.left[:-1] >= self.right[1:]).all())
if self.closed == 'both':
return ((self.right[:-1] < self.left[1:]).all() or
(self.left[:-1] > self.right[1:]).all())
else:
Copy link
Contributor

Choose a reason for hiding this comment

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

don't use the else here

add a comment on these cases (left/right).

neighter case? (short circuit to False)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Working on this now. Why would the neither case short circuit to False? Can't we have intervals [(0, 1), (1, 2)] where it is non-overlapping and also cases where it is overlapping [(0, 1), (.5, 3)]

Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

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

this is checking monotonicity as well. so you can do the same for neither.

return ((self.right[:-1] <= self.left[1:]).all() or
(self.left[:-1] >= self.right[1:]).all())

@Appender(_index_shared_docs['_convert_scalar_indexer'])
def _convert_scalar_indexer(self, key, kind=None):
Expand Down
13 changes: 11 additions & 2 deletions pandas/tests/indexes/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,19 @@ def slice_locs_cases(self, breaks):
assert index.slice_locs(1, 1) == (1, 1)
assert index.slice_locs(1, 2) == (1, 2)

index = IntervalIndex.from_breaks([0, 1, 2], closed='both')
assert index.slice_locs(1, 1) == (0, 2)
index = IntervalIndex.from_tuples(
[(0, 1), (2, 3), (4, 5)], closed='both')
assert index.slice_locs(1, 1) == (0, 1)
assert index.slice_locs(1, 2) == (0, 2)

def test_is_non_overlapping_monotonic(self):
index = IntervalIndex.from_tuples(
[(0, 1), (2, 3), (4, 5)], closed='both')
assert index.is_non_overlapping_monotonic == True
Copy link
Contributor

Choose a reason for hiding this comment

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

use is True


index = IntervalIndex.from_breaks(range(4), closed='both')
assert index.is_non_overlapping_monotonic == False
Copy link
Contributor

Choose a reason for hiding this comment

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

use is False


def test_slice_locs_int64(self):
self.slice_locs_cases([0, 1, 2])

Expand Down