Skip to content

Commit e5a90fa

Browse files
committed
NH: gb.is_monotonic_increasing pandas-dev#17015 alternate version to remove function defs
1 parent fb0bc3c commit e5a90fa

File tree

3 files changed

+8
-62
lines changed

3 files changed

+8
-62
lines changed

pandas/core/groupby.py

+3-58
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,9 @@
281281
]) | _plotting_methods
282282

283283
_series_apply_whitelist = ((_common_apply_whitelist |
284-
{'nlargest', 'nsmallest'}) -
284+
{'nlargest', 'nsmallest',
285+
'is_monotonic_increasing',
286+
'is_monotonic_decreasing'}) -
285287
{'boxplot'}) | frozenset(['dtype', 'unique'])
286288

287289
_dataframe_apply_whitelist = ((_common_apply_whitelist |
@@ -1826,63 +1828,6 @@ def pipe(self, func, *args, **kwargs):
18261828
"""
18271829
return _pipe(self, func, *args, **kwargs)
18281830

1829-
@Substitution(name='groupby')
1830-
@Appender(_doc_template)
1831-
def is_monotonic_increasing(self):
1832-
"""
1833-
Returns whether each group is monotonically increasing.
1834-
1835-
Equivalent to ``.apply(lambda x: x.is_monotonic_increasing)``.
1836-
1837-
.. versionadded:: 0.22.0
1838-
1839-
Examples
1840-
--------
1841-
>>> source_dict = {
1842-
... 'A': ['this', 'col', 'is', 'entirely', 'irrelevant', '.'],
1843-
... 'B': ['cat_a', 'cat_a', 'cat_a', 'cat_b', 'cat_b', 'cat_b'],
1844-
... 'C': [1, 2, 3, 2, 2, 0]}
1845-
1846-
>>> df = pd.DataFrame(source_dict)
1847-
... df.groupby(['B']).C.is_monotonic_increasing()
1848-
B
1849-
cat_a True
1850-
cat_b False
1851-
Name: C, dtype: bool
1852-
1853-
See Also
1854-
--------
1855-
pandas.Series.is_monotonic_increasing
1856-
pandas.Index.is_monotonic_increasing
1857-
"""
1858-
return self.apply(lambda x: x.is_monotonic_increasing)
1859-
1860-
@Substitution(name='groupby')
1861-
@Appender(_doc_template)
1862-
def is_monotonic_decreasing(self):
1863-
"""
1864-
Returns whether each group is monotonically decreasing.
1865-
1866-
Equivalent to ``.apply(lambda x: x.is_monotonic_decreasing)``.
1867-
1868-
.. versionadded:: 0.22.0
1869-
1870-
Examples
1871-
--------
1872-
>>> source_dict = {
1873-
... 'A': ['this', 'col', 'is', 'entirely', 'irrelevant', '.'],
1874-
... 'B': ['cat_a', 'cat_a', 'cat_a', 'cat_b', 'cat_b', 'cat_b'],
1875-
... 'C': [1, 2, 3, 2, 2, 0]}
1876-
1877-
>>> df = pd.DataFrame(source_dict)
1878-
... df.groupby(['B']).C.is_monotonic_decreasing()
1879-
B
1880-
cat_a False
1881-
cat_b True
1882-
Name: C, dtype: bool
1883-
"""
1884-
return self.apply(lambda x: x.is_monotonic_decreasing)
1885-
18861831

18871832
GroupBy._add_numeric_operations()
18881833

pandas/tests/groupby/test_groupby.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2622,13 +2622,13 @@ def test_is_monotonic_increasing(self, in_vals, out_vals):
26222622
'B': ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd'],
26232623
'C': in_vals}
26242624
df = pd.DataFrame(source_dict)
2625-
result = df.groupby('B').C.is_monotonic_increasing()
2625+
result = df.groupby('B').C.is_monotonic_increasing
26262626
index = Index(list('abcd'), name='B')
26272627
expected = pd.Series(index=index, data=out_vals, name='C')
26282628
tm.assert_series_equal(result, expected)
26292629

26302630
# Also check result equal to manually taking x.is_monotonic_increasing.
2631-
expected = (
2631+
expecteAd = (
26322632
df.groupby(['B']).C.apply(lambda x: x.is_monotonic_increasing))
26332633
tm.assert_series_equal(result, expected)
26342634

@@ -2654,7 +2654,7 @@ def test_is_monotonic_decreasing(self, in_vals, out_vals):
26542654
'C': in_vals}
26552655

26562656
df = pd.DataFrame(source_dict)
2657-
result = df.groupby('B').C.is_monotonic_decreasing()
2657+
result = df.groupby('B').C.is_monotonic_decreasing
26582658
index = Index(list('abcd'), name='B')
26592659
expected = pd.Series(index=index, data=out_vals, name='C')
26602660
tm.assert_series_equal(result, expected)

pandas/tests/groupby/test_whitelist.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@
8888
'unique',
8989
'nlargest',
9090
'nsmallest',
91+
'is_monotonic_increasing',
92+
'is_monotonic_decreasing',
9193
])
9294

9395

@@ -250,7 +252,6 @@ def test_tab_completion(mframe):
250252
'take', 'tshift', 'pct_change', 'any', 'mad', 'corr', 'corrwith',
251253
'cov', 'dtypes', 'ndim', 'diff', 'idxmax', 'idxmin',
252254
'ffill', 'bfill', 'pad', 'backfill', 'rolling', 'expanding', 'pipe',
253-
'is_monotonic_increasing', 'is_monotonic_decreasing'
254255
}
255256
assert results == expected
256257

0 commit comments

Comments
 (0)