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 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
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ Bug Fixes
Conversion
^^^^^^^^^^

- Bug in ``IntervalIndex`` where ``is_non_overlapping_monotonic`` returned incorrect value for intervals closed on both sides (:issue:`16560`)


Indexing
Expand Down
5 changes: 5 additions & 0 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,11 @@ 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
if self.closed == 'both':
return ((self.right[:-1] < self.left[1:]).all() or
(self.left[:-1] > self.right[1:]).all())

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

Expand Down
30 changes: 28 additions & 2 deletions pandas/tests/indexes/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,36 @@ 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

index = IntervalIndex.from_breaks([0, 1, 2], closed='neither')
assert index.is_non_overlapping_monotonic == True
index = IntervalIndex.from_tuples(
[(0, 2), (1, 3), (3, 4)], closed='neither')
assert index.is_non_overlapping_monotonic == False

index = IntervalIndex.from_breaks(range(4), closed='left')
assert index.is_non_overlapping_monotonic == True
index = IntervalIndex.from_tuples(
[(0, 1), (1, 2), (1.5, 3)], closed='left')
assert index.is_non_overlapping_monotonic == False

index = IntervalIndex.from_breaks(range(4), closed='right')
assert index.is_non_overlapping_monotonic == True
index = IntervalIndex.from_tuples(
[(0, 1), (1, 2), (1.5, 3)], closed='right')
assert index.is_non_overlapping_monotonic == False

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

Expand Down