From 0a50ff924d8ba97a09aeda52d6318e89ffa53862 Mon Sep 17 00:00:00 2001 From: Jeff Reback Date: Tue, 31 May 2016 16:47:25 -0400 Subject: [PATCH] ENH: Series has gained the properties .is_monotonic, .is_monotonic_increasing, .is_monotonic_decreasing --- doc/source/api.rst | 3 +++ doc/source/whatsnew/v0.18.2.txt | 2 +- pandas/core/base.py | 26 ++++++++++++++++++++++++++ pandas/tests/series/test_analytics.py | 17 +++++++++++++++++ 4 files changed, 47 insertions(+), 1 deletion(-) diff --git a/doc/source/api.rst b/doc/source/api.rst index 9e7ae2357c541..0e893308dd935 100644 --- a/doc/source/api.rst +++ b/doc/source/api.rst @@ -354,6 +354,9 @@ Computations / Descriptive Stats Series.unique Series.nunique Series.is_unique + Series.is_monotonic + Series.is_monotonic_increasing + Series.is_monotonic_decreasing Series.value_counts Reindexing / Selection / Label manipulation diff --git a/doc/source/whatsnew/v0.18.2.txt b/doc/source/whatsnew/v0.18.2.txt index 33a48671a9b65..3fc1a69cb600e 100644 --- a/doc/source/whatsnew/v0.18.2.txt +++ b/doc/source/whatsnew/v0.18.2.txt @@ -92,7 +92,7 @@ Other enhancements - ``pd.read_html()`` has gained support for the ``decimal`` option (:issue:`12907`) - ``eval``'s upcasting rules for ``float32`` types have been updated to be more consistent with NumPy's rules. New behavior will not upcast to ``float64`` if you multiply a pandas ``float32`` object by a scalar float64. (:issue:`12388`) - +- ``Series`` has gained the properties ``.is_monotonic``, ``.is_monotonic_increasing``, ``.is_monotonic_decreasing``, similar to ``Index`` (:issue:`13336`) .. _whatsnew_0182.api: diff --git a/pandas/core/base.py b/pandas/core/base.py index 36f1f24fec6f7..bf21455bfac91 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -995,6 +995,32 @@ def is_unique(self): """ return self.nunique() == len(self) + @property + def is_monotonic(self): + """ + Return boolean if values in the object are monotonic + + Returns + ------- + is_monotonic : boolean + """ + from pandas import Index + return Index(self).is_monotonic + is_monotonic_increasing = is_monotonic + + @property + def is_monotonic_decreasing(self): + """ + Return boolean if values in the object are + monotonic_decreasing + + Returns + ------- + is_monotonic_decreasing : boolean + """ + from pandas import Index + return Index(self).is_monotonic_decreasing + def memory_usage(self, deep=False): """ Memory usage of my values diff --git a/pandas/tests/series/test_analytics.py b/pandas/tests/series/test_analytics.py index c190b0d9e3bb0..433f0f4bc67f5 100644 --- a/pandas/tests/series/test_analytics.py +++ b/pandas/tests/series/test_analytics.py @@ -1397,6 +1397,23 @@ def test_is_unique(self): s = Series(np.arange(1000)) self.assertTrue(s.is_unique) + def test_is_monotonic(self): + + s = Series(np.random.randint(0, 10, size=1000)) + self.assertFalse(s.is_monotonic) + s = Series(np.arange(1000)) + self.assertTrue(s.is_monotonic) + self.assertTrue(s.is_monotonic_increasing) + s = Series(np.arange(1000, 0, -1)) + self.assertTrue(s.is_monotonic_decreasing) + + s = Series(pd.date_range('20130101', periods=10)) + self.assertTrue(s.is_monotonic) + self.assertTrue(s.is_monotonic_increasing) + s = Series(list(reversed(s.tolist()))) + self.assertFalse(s.is_monotonic) + self.assertTrue(s.is_monotonic_decreasing) + def test_sort_values(self): ts = self.ts.copy()