Skip to content

Commit 3fdcea6

Browse files
sinhrksjreback
authored andcommitted
PERF: RangeIndex.is_monotonic_inc/dec
Author: sinhrks <[email protected]> Closes pandas-dev#13749 from sinhrks/perf_range and squashes the following commits: 8e25563 [sinhrks] PERF: RangeIndex.is_monotonic_inc/dec
1 parent 309e1fe commit 3fdcea6

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

doc/source/whatsnew/v0.19.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,7 @@ Performance Improvements
659659
- Improved performance of ``DataFrameGroupBy.transform`` (:issue:`12737`)
660660
- Improved performance of ``Index`` and ``Series`` ``.duplicated`` (:issue:`10235`)
661661
- Improved performance of ``Index.difference`` (:issue:`12044`)
662+
- Improved performance of ``RangeIndex.is_monotonic_increasing`` and ``is_monotonic_decreasing`` (:issue:`13749`)
662663
- Improved performance of datetime string parsing in ``DatetimeIndex`` (:issue:`13692`)
663664
- Improved performance of hashing ``Period`` (:issue:`12817`)
664665
- Improved performance of ``factorize`` of datetime with timezone (:issue:`13750`)

pandas/indexes/range.py

+8
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,14 @@ def is_unique(self):
221221
""" return if the index has unique values """
222222
return True
223223

224+
@cache_readonly
225+
def is_monotonic_increasing(self):
226+
return self._step > 0 or len(self) <= 1
227+
228+
@cache_readonly
229+
def is_monotonic_decreasing(self):
230+
return self._step < 0 or len(self) <= 1
231+
224232
@property
225233
def has_duplicates(self):
226234
return False

pandas/tests/indexes/test_range.py

+10
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,16 @@ def test_is_monotonic(self):
315315
self.assertTrue(index.is_monotonic_increasing)
316316
self.assertTrue(index.is_monotonic_decreasing)
317317

318+
index = RangeIndex(2, 1)
319+
self.assertTrue(index.is_monotonic)
320+
self.assertTrue(index.is_monotonic_increasing)
321+
self.assertTrue(index.is_monotonic_decreasing)
322+
323+
index = RangeIndex(1, 1)
324+
self.assertTrue(index.is_monotonic)
325+
self.assertTrue(index.is_monotonic_increasing)
326+
self.assertTrue(index.is_monotonic_decreasing)
327+
318328
def test_equals(self):
319329
equiv_pairs = [(RangeIndex(0, 9, 2), RangeIndex(0, 10, 2)),
320330
(RangeIndex(0), RangeIndex(1, -1, 3)),

0 commit comments

Comments
 (0)