Skip to content

Commit 8bbd2bc

Browse files
committed
ENH: Series has gained the properties .is_monotonic*
Author: Jeff Reback <[email protected]> Closes #13336 from jreback/is_monotonic and squashes the following commits: 0a50ff9 [Jeff Reback] ENH: Series has gained the properties .is_monotonic, .is_monotonic_increasing, .is_monotonic_decreasing
1 parent f3d7c18 commit 8bbd2bc

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

doc/source/api.rst

+3
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,9 @@ Computations / Descriptive Stats
354354
Series.unique
355355
Series.nunique
356356
Series.is_unique
357+
Series.is_monotonic
358+
Series.is_monotonic_increasing
359+
Series.is_monotonic_decreasing
357360
Series.value_counts
358361

359362
Reindexing / Selection / Label manipulation

doc/source/whatsnew/v0.18.2.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ Other enhancements
9292
- ``pd.read_html()`` has gained support for the ``decimal`` option (:issue:`12907`)
9393

9494
- ``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`)
95-
95+
- ``Series`` has gained the properties ``.is_monotonic``, ``.is_monotonic_increasing``, ``.is_monotonic_decreasing``, similar to ``Index`` (:issue:`13336`)
9696

9797
.. _whatsnew_0182.api:
9898

pandas/core/base.py

+31
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,37 @@ def is_unique(self):
995995
"""
996996
return self.nunique() == len(self)
997997

998+
@property
999+
def is_monotonic(self):
1000+
"""
1001+
Return boolean if values in the object are
1002+
monotonic_increasing
1003+
1004+
.. versionadded:: 0.18.2
1005+
1006+
Returns
1007+
-------
1008+
is_monotonic : boolean
1009+
"""
1010+
from pandas import Index
1011+
return Index(self).is_monotonic
1012+
is_monotonic_increasing = is_monotonic
1013+
1014+
@property
1015+
def is_monotonic_decreasing(self):
1016+
"""
1017+
Return boolean if values in the object are
1018+
monotonic_decreasing
1019+
1020+
.. versionadded:: 0.18.2
1021+
1022+
Returns
1023+
-------
1024+
is_monotonic_decreasing : boolean
1025+
"""
1026+
from pandas import Index
1027+
return Index(self).is_monotonic_decreasing
1028+
9981029
def memory_usage(self, deep=False):
9991030
"""
10001031
Memory usage of my values

pandas/tests/series/test_analytics.py

+17
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,23 @@ def test_is_unique(self):
13971397
s = Series(np.arange(1000))
13981398
self.assertTrue(s.is_unique)
13991399

1400+
def test_is_monotonic(self):
1401+
1402+
s = Series(np.random.randint(0, 10, size=1000))
1403+
self.assertFalse(s.is_monotonic)
1404+
s = Series(np.arange(1000))
1405+
self.assertTrue(s.is_monotonic)
1406+
self.assertTrue(s.is_monotonic_increasing)
1407+
s = Series(np.arange(1000, 0, -1))
1408+
self.assertTrue(s.is_monotonic_decreasing)
1409+
1410+
s = Series(pd.date_range('20130101', periods=10))
1411+
self.assertTrue(s.is_monotonic)
1412+
self.assertTrue(s.is_monotonic_increasing)
1413+
s = Series(list(reversed(s.tolist())))
1414+
self.assertFalse(s.is_monotonic)
1415+
self.assertTrue(s.is_monotonic_decreasing)
1416+
14001417
def test_sort_values(self):
14011418

14021419
ts = self.ts.copy()

0 commit comments

Comments
 (0)