Skip to content

Commit 29510e8

Browse files
committed
Fix bugs in IntervalIndex.is_non_overlapping_monotonic
IntervalIndex.is_non_overlapping_monotonic returns a Python bool instead of numpy.bool_, and correctly handles the closed='both' case where endpoints are shared.
1 parent 330b8c1 commit 29510e8

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

doc/source/whatsnew/v0.21.0.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ Conversion
309309

310310
- Bug in assignment against datetime-like data with ``int`` may incorrectly converte to datetime-like (:issue:`14145`)
311311
- Bug in assignment against ``int64`` data with ``np.ndarray`` with ``float64`` dtype may keep ``int64`` dtype (:issue:`14001`)
312-
312+
- Bug in the return type of ``IntervalIndex.is_non_overlapping_monotonic``, which returned ``numpy.bool_`` instead of Python ``bool`` (:issue:`17237`)
313313

314314
Indexing
315315
^^^^^^^^
@@ -385,3 +385,4 @@ Other
385385
- Bug in :func:`eval` where the ``inplace`` parameter was being incorrectly handled (:issue:`16732`)
386386
- Bug in ``.isin()`` in which checking membership in empty ``Series`` objects raised an error (:issue:`16991`)
387387
- Bug in :func:`unique` where checking a tuple of strings raised a ``TypeError`` (:issue:`17108`)
388+
- Bug in ``IntervalIndex.is_non_overlapping_monotonic`` when intervals are closed on both sides and overlap at a point (:issue:`16560`)

pandas/core/indexes/interval.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -556,8 +556,17 @@ def is_non_overlapping_monotonic(self):
556556
# must be increasing (e.g., [0, 1), [1, 2), [2, 3), ... )
557557
# or decreasing (e.g., [-1, 0), [-2, -1), [-3, -2), ...)
558558
# we already require left <= right
559-
return ((self.right[:-1] <= self.left[1:]).all() or
560-
(self.left[:-1] >= self.right[1:]).all())
559+
560+
# strict inequality for closed == 'both'; equality implies overlapping
561+
# at a point when both sides of intervals are included
562+
if self.closed == 'both':
563+
return bool((self.right[:-1] < self.left[1:]).all() or
564+
(self.left[:-1] > self.right[1:]).all())
565+
566+
# non-strict inequality when closed != 'both'; at least one side is
567+
# not included in the intervals, so equality does not imply overlapping
568+
return bool((self.right[:-1] <= self.left[1:]).all() or
569+
(self.left[:-1] >= self.right[1:]).all())
561570

562571
@Appender(_index_shared_docs['_convert_scalar_indexer'])
563572
def _convert_scalar_indexer(self, key, kind=None):

pandas/tests/indexes/test_interval.py

+44-2
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,9 @@ def slice_locs_cases(self, breaks):
371371
assert index.slice_locs(1, 1) == (1, 1)
372372
assert index.slice_locs(1, 2) == (1, 2)
373373

374-
index = IntervalIndex.from_breaks([0, 1, 2], closed='both')
375-
assert index.slice_locs(1, 1) == (0, 2)
374+
index = IntervalIndex.from_tuples([(0, 1), (2, 3), (4, 5)],
375+
closed='both')
376+
assert index.slice_locs(1, 1) == (0, 1)
376377
assert index.slice_locs(1, 2) == (0, 2)
377378

378379
def test_slice_locs_int64(self):
@@ -681,6 +682,47 @@ def f():
681682

682683
pytest.raises(ValueError, f)
683684

685+
def test_is_non_overlapping_monotonic(self):
686+
# Verify that a Python Boolean is returned (GH17237)
687+
for closed in ('left', 'right', 'neither', 'both'):
688+
idx = IntervalIndex.from_breaks(range(4), closed=closed)
689+
assert type(idx.is_non_overlapping_monotonic) is bool
690+
691+
# Should be True in all cases
692+
tpls = [(0, 1), (2, 3), (4, 5), (6, 7)]
693+
for closed in ('left', 'right', 'neither', 'both'):
694+
idx = IntervalIndex.from_tuples(tpls, closed=closed)
695+
assert idx.is_non_overlapping_monotonic is True
696+
697+
idx = IntervalIndex.from_tuples(reversed(tpls), closed=closed)
698+
assert idx.is_non_overlapping_monotonic is True
699+
700+
# Should be False in all cases (overlapping)
701+
tpls = [(0, 2), (1, 3), (4, 5), (6, 7)]
702+
for closed in ('left', 'right', 'neither', 'both'):
703+
idx = IntervalIndex.from_tuples(tpls, closed=closed)
704+
assert idx.is_non_overlapping_monotonic is False
705+
706+
idx = IntervalIndex.from_tuples(reversed(tpls), closed=closed)
707+
assert idx.is_non_overlapping_monotonic is False
708+
709+
# Should be False in all cases (non-monotonic)
710+
tpls = [(0, 1), (2, 3), (6, 7), (4, 5)]
711+
for closed in ('left', 'right', 'neither', 'both'):
712+
idx = IntervalIndex.from_tuples(tpls, closed=closed)
713+
assert idx.is_non_overlapping_monotonic is False
714+
715+
idx = IntervalIndex.from_tuples(reversed(tpls), closed=closed)
716+
assert idx.is_non_overlapping_monotonic is False
717+
718+
# Should be False for closed='both', overwise True (GH16560)
719+
idx = IntervalIndex.from_breaks(range(4), closed='both')
720+
assert idx.is_non_overlapping_monotonic is False
721+
722+
for closed in ('left', 'right', 'neither'):
723+
idx = IntervalIndex.from_breaks(range(4), closed=closed)
724+
assert idx.is_non_overlapping_monotonic is True
725+
684726

685727
class TestIntervalRange(object):
686728

0 commit comments

Comments
 (0)