Skip to content

Commit e99897c

Browse files
committed
ENH: gb.is_monotonic_increasing, is_monotonic_decreasing pandas-dev#17015
1 parent 5bca6ce commit e99897c

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

doc/source/whatsnew/v0.21.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ Other Enhancements
130130
- `read_*` methods can now infer compression from non-string paths, such as ``pathlib.Path`` objects (:issue:`17206`).
131131
- :func:`pd.read_sas()` now recognizes much more of the most frequently used date (datetime) formats in SAS7BDAT files (:issue:`15871`).
132132
- :func:`DataFrame.items` and :func:`Series.items` is now present in both Python 2 and 3 and is lazy in all cases (:issue:`13918`, :issue:`17213`)
133-
133+
- :func: groupby.is_monotonic_increasing and .is_monotonic_decreasing extend Series.is_monotonic_increasing to groups, returning whether each group is monotonically increasing or decreasing, respectively. (:issue:`17015`)
134134

135135

136136
.. _whatsnew_0210.api_breaking:

pandas/core/groupby.py

+49
Original file line numberDiff line numberDiff line change
@@ -1686,6 +1686,55 @@ def tail(self, n=5):
16861686
mask = self._cumcount_array(ascending=False) < n
16871687
return self._selected_obj[mask]
16881688

1689+
@Substitution(name='groupby')
1690+
@Appender(_doc_template)
1691+
def is_monotonic_increasing(self):
1692+
"""
1693+
Returns whether each group is monotonically increasing.
1694+
1695+
Equivalent to ``.apply(lambda x: x.is_monotonic_increasing)``.
1696+
1697+
Examples
1698+
--------
1699+
>>> source_dict = {
1700+
... 'A': ['this', 'col', 'is', 'entirely', 'irrelevant', '.'],
1701+
... 'B': ['cat_a', 'cat_a', 'cat_a', 'cat_b', 'cat_b', 'cat_b'],
1702+
... 'C': [1, 2, 3, 2, 2, 0]}
1703+
1704+
>>> df = pd.DataFrame(source_dict)
1705+
... df.groupby(['B']).C.is_monotonic_increasing()
1706+
B
1707+
cat_a True
1708+
cat_b False
1709+
Name: C, dtype: bool
1710+
1711+
"""
1712+
return self.apply(lambda x: x.is_monotonic_increasing)
1713+
1714+
@Substitution(name='groupby')
1715+
@Appender(_doc_template)
1716+
def is_monotonic_decreasing(self):
1717+
"""
1718+
Returns whether each group is monotonically decreasing.
1719+
1720+
Equivalent to ``.apply(lambda x: x.is_monotonic_decreasing)``.
1721+
1722+
Examples
1723+
--------
1724+
>>> source_dict = {
1725+
... 'A': ['this', 'col', 'is', 'entirely', 'irrelevant', '.'],
1726+
... 'B': ['cat_a', 'cat_a', 'cat_a', 'cat_b', 'cat_b', 'cat_b'],
1727+
... 'C': [1, 2, 3, 2, 2, 0]}
1728+
1729+
>>> df = pd.DataFrame(source_dict)
1730+
... df.groupby(['B']).C.is_monotonic_decreasing()
1731+
B
1732+
cat_a False
1733+
cat_b True
1734+
Name: C, dtype: bool
1735+
"""
1736+
return self.apply(lambda x: x.is_monotonic_decreasing)
1737+
16891738

16901739
GroupBy._add_numeric_operations()
16911740

pandas/tests/groupby/test_whitelist.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ def test_tab_completion(mframe):
241241
'cumsum', 'cumcount', 'ngroup', 'all', 'shift', 'skew',
242242
'take', 'tshift', 'pct_change', 'any', 'mad', 'corr', 'corrwith',
243243
'cov', 'dtypes', 'ndim', 'diff', 'idxmax', 'idxmin',
244-
'ffill', 'bfill', 'pad', 'backfill', 'rolling', 'expanding'])
244+
'ffill', 'bfill', 'pad', 'backfill', 'rolling', 'expanding',
245+
'is_monotonic_increasing', 'is_monotonic_decreasing'])
245246
assert results == expected
246247

247248

0 commit comments

Comments
 (0)