Skip to content

Commit 8e25563

Browse files
committed
PERF: RangeIndex.is_monotonic_inc/dec
1 parent 5a3b071 commit 8e25563

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

doc/source/whatsnew/v0.19.0.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,7 @@ Performance Improvements
649649
- Improved performance of float64 hash table operations, fixing some very slow indexing and groupby operations in python 3 (:issue:`13166`, :issue:`13334`)
650650
- Improved performance of ``DataFrameGroupBy.transform`` (:issue:`12737`)
651651
- Improved performance of ``Index.difference`` (:issue:`12044`)
652+
- Improved performance of ``RangeIndex.is_monotonic_increasing`` and ``is_monotonic_decreasing`` (:issue:`13749`)
652653
- Improved performance of datetime string parsing in ``DatetimeIndex`` (:issue:`13692`)
653654
- Improved performance of hashing ``Period`` (:issue:`12817`)
654655

pandas/indexes/range.py

Lines changed: 8 additions & 0 deletions
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

Lines changed: 10 additions & 0 deletions
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)