Skip to content

Commit

Permalink
PERF: RangeIndex.is_monotonic_inc/dec
Browse files Browse the repository at this point in the history
Author: sinhrks <[email protected]>

Closes #13749 from sinhrks/perf_range and squashes the following commits:

8e25563 [sinhrks] PERF: RangeIndex.is_monotonic_inc/dec
  • Loading branch information
sinhrks authored and jreback committed Jul 25, 2016
1 parent 309e1fe commit 3fdcea6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.19.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,7 @@ Performance Improvements
- Improved performance of ``DataFrameGroupBy.transform`` (:issue:`12737`)
- Improved performance of ``Index`` and ``Series`` ``.duplicated`` (:issue:`10235`)
- Improved performance of ``Index.difference`` (:issue:`12044`)
- Improved performance of ``RangeIndex.is_monotonic_increasing`` and ``is_monotonic_decreasing`` (:issue:`13749`)
- Improved performance of datetime string parsing in ``DatetimeIndex`` (:issue:`13692`)
- Improved performance of hashing ``Period`` (:issue:`12817`)
- Improved performance of ``factorize`` of datetime with timezone (:issue:`13750`)
Expand Down
8 changes: 8 additions & 0 deletions pandas/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,14 @@ def is_unique(self):
""" return if the index has unique values """
return True

@cache_readonly
def is_monotonic_increasing(self):
return self._step > 0 or len(self) <= 1

@cache_readonly
def is_monotonic_decreasing(self):
return self._step < 0 or len(self) <= 1

@property
def has_duplicates(self):
return False
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/indexes/test_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,16 @@ def test_is_monotonic(self):
self.assertTrue(index.is_monotonic_increasing)
self.assertTrue(index.is_monotonic_decreasing)

index = RangeIndex(2, 1)
self.assertTrue(index.is_monotonic)
self.assertTrue(index.is_monotonic_increasing)
self.assertTrue(index.is_monotonic_decreasing)

index = RangeIndex(1, 1)
self.assertTrue(index.is_monotonic)
self.assertTrue(index.is_monotonic_increasing)
self.assertTrue(index.is_monotonic_decreasing)

def test_equals(self):
equiv_pairs = [(RangeIndex(0, 9, 2), RangeIndex(0, 10, 2)),
(RangeIndex(0), RangeIndex(1, -1, 3)),
Expand Down

0 comments on commit 3fdcea6

Please sign in to comment.