ENH: gb.is_monotonic_increasing · Issue #17015 · pandas-dev/pandas (original) (raw)

Code Sample, a copy-pastable example if possible

source_dict = { 'A': ['foo', 'bar', 'baz', 'foo', 'bar', 'baz'], 'B': ['cat_a', 'cat_a', 'cat_a', 'cat_b', 'cat_b', 'cat_b'], 'C': [1, 2, 3, 2, 2, 0] }

example = pd.DataFrame(source_dict) example.groupby(['B']).C.apply(lambda x: x.is_monotonic_decreasing)

Output:

B

cat_a False

cat_b True

Name: C, dtype: bool

Problem description

In order to check which groups are monotonically increasing or decreasing, you can currently use gb.apply(lambda x: x.is_monotonic_increasing).

What I'm suggesting is that Series.is_monotonic_increasing be extended to groupby, so there would be a groupby.is_monotonic_increasing() method, which would return the same output as the apply.

I'm not sure if extending a Series property to a Groupby method is something that makes sense for Pandas, and I haven't contributed to Pandas before, but I wanted to ask.

Expected Output

example.groupby(['B']).C.is_monotonic_decreasing()
# Output:
# B
# cat_a    False
# cat_b     True
# Name: C, dtype: bool

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 2.7.13.final.0 python-bits: 64 OS: Darwin OS-release: 16.6.0 machine: x86_64 processor: i386 byteorder: little LC_ALL: None LANG: None LOCALE: None.None pandas: 0.20.2 pytest: 2.9.2 pip: 9.0.1 setuptools: 28.3.0 Cython: 0.24.1 numpy: 1.13.0 scipy: 0.19.1 xarray: None IPython: 5.1.0 sphinx: 1.4.6 patsy: 0.4.1 dateutil: 2.6.0 pytz: 2017.2 blosc: None bottleneck: 1.2.1 tables: 3.3.0 numexpr: 2.6.2 feather: None matplotlib: 2.0.2 openpyxl: 2.3.2 xlrd: 1.0.0 xlwt: 1.1.2 xlsxwriter: 0.9.3 lxml: 3.6.4 bs4: 4.5.1 html5lib: None sqlalchemy: 1.0.13 pymysql: None psycopg2: None jinja2: 2.8 s3fs: None pandas_gbq: None pandas_datareader: None