Skip to content

ENH: Series has gained the properties .is_monotonic* #13336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.18.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
26 changes: 26 additions & 0 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/series/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down