From 8e25563bc9061535853c19740684a7d40394cc33 Mon Sep 17 00:00:00 2001 From: sinhrks Date: Fri, 22 Jul 2016 05:21:38 +0900 Subject: [PATCH] PERF: RangeIndex.is_monotonic_inc/dec --- doc/source/whatsnew/v0.19.0.txt | 1 + pandas/indexes/range.py | 8 ++++++++ pandas/tests/indexes/test_range.py | 10 ++++++++++ 3 files changed, 19 insertions(+) diff --git a/doc/source/whatsnew/v0.19.0.txt b/doc/source/whatsnew/v0.19.0.txt index 721da38baf67d..dcf28ae54094d 100644 --- a/doc/source/whatsnew/v0.19.0.txt +++ b/doc/source/whatsnew/v0.19.0.txt @@ -649,6 +649,7 @@ Performance Improvements - Improved performance of float64 hash table operations, fixing some very slow indexing and groupby operations in python 3 (:issue:`13166`, :issue:`13334`) - Improved performance of ``DataFrameGroupBy.transform`` (:issue:`12737`) - 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`) diff --git a/pandas/indexes/range.py b/pandas/indexes/range.py index f680d2da0161e..a561cab30b472 100644 --- a/pandas/indexes/range.py +++ b/pandas/indexes/range.py @@ -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 diff --git a/pandas/tests/indexes/test_range.py b/pandas/tests/indexes/test_range.py index 99e4b72bcee37..329ffa9b7cc77 100644 --- a/pandas/tests/indexes/test_range.py +++ b/pandas/tests/indexes/test_range.py @@ -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)),