Skip to content
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

ENH: gb.is_monotonic_increasing #17015

Closed
No-Stream opened this issue Jul 18, 2017 · 3 comments
Closed

ENH: gb.is_monotonic_increasing #17015

No-Stream opened this issue Jul 18, 2017 · 3 comments

Comments

@No-Stream
Copy link
Contributor

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
@gfyoung
Copy link
Member

gfyoung commented Jul 19, 2017

@No-Stream : I'm not sure I follow your output here. Can you explain why you get False and True. What are you checking for monotonicity?

@No-Stream
Copy link
Contributor Author

@gfyoung I should have explained a bit more but glossed over the details since the suggested enhancement is basically just syntactic sugar. Gb.apply(lambda x: x.seriesproperty) looks ugly to me. I'd rather have gb.gbmethod()

In the example, category a (cat_a) is monotonically increasing (so not monotonically decreasing) and category b is in fact categorically decreasing. So you get False, True. This behavior is correct.

@jreback
Copy link
Contributor

jreback commented Jul 19, 2017

your .apply soln is fine generally. I suppose this could be added. If you wanted to do a PR I think would be ok.

No-Stream added a commit to No-Stream/pandas that referenced this issue Oct 31, 2017
No-Stream added a commit to No-Stream/pandas that referenced this issue Oct 31, 2017
No-Stream added a commit to No-Stream/pandas that referenced this issue Oct 31, 2017
No-Stream added a commit to No-Stream/pandas that referenced this issue Oct 31, 2017
No-Stream added a commit to No-Stream/pandas that referenced this issue Oct 31, 2017
jreback pushed a commit to No-Stream/pandas that referenced this issue Nov 1, 2017
jreback pushed a commit to No-Stream/pandas that referenced this issue Nov 1, 2017
jreback pushed a commit to No-Stream/pandas that referenced this issue Nov 1, 2017
jreback pushed a commit to No-Stream/pandas that referenced this issue Nov 1, 2017
@jreback jreback added this to the 0.21.1 milestone Nov 10, 2017
No-Stream pushed a commit to No-Stream/pandas that referenced this issue Nov 14, 2017
…ev#18269)

ENH: gb.is_monotonic_increasing pandas-dev#17015 fix rebase conflicts

parametrized tests for gb.is_monotonic_increasing/decreasing

ENH: gb.is_monotonic_increasing, is_monotonic_decreasing  pandas-dev#17015

added tests for gb.is_monotonically_increasing()/decreasing

parametrized tests for gb.is_monotonic_increasing/decreasing

ENH: gb.is_monotonic_increasing pandas-dev#17015 fix rebase conflicts

ENH: gb.is_monotonic_increasing pandas-dev#17015 fix rebase conflicts

rebase and cleanup

simplified test format

fixed whatsnew to include method tags
No-Stream added a commit to No-Stream/pandas that referenced this issue Nov 14, 2017
No-Stream added a commit to No-Stream/pandas that referenced this issue Nov 14, 2017
No-Stream added a commit to No-Stream/pandas that referenced this issue Nov 28, 2017
No-Stream added a commit to No-Stream/pandas that referenced this issue Nov 28, 2017
No-Stream added a commit to No-Stream/pandas that referenced this issue Nov 28, 2017
No-Stream added a commit to No-Stream/pandas that referenced this issue Nov 28, 2017
No-Stream added a commit to No-Stream/pandas that referenced this issue Nov 28, 2017
No-Stream added a commit to No-Stream/pandas that referenced this issue Nov 28, 2017
No-Stream added a commit to No-Stream/pandas that referenced this issue Nov 29, 2017
No-Stream added a commit to No-Stream/pandas that referenced this issue Nov 29, 2017
@jorisvandenbossche jorisvandenbossche modified the milestones: 0.21.1, 0.22.0 Nov 30, 2017
No-Stream added a commit to No-Stream/pandas that referenced this issue Dec 12, 2017
No-Stream added a commit to No-Stream/pandas that referenced this issue Dec 12, 2017
No-Stream added a commit to No-Stream/pandas that referenced this issue Dec 12, 2017
No-Stream added a commit to No-Stream/pandas that referenced this issue Dec 12, 2017
No-Stream added a commit to No-Stream/pandas that referenced this issue Dec 15, 2017
No-Stream added a commit to No-Stream/pandas that referenced this issue Dec 15, 2017
harisbal pushed a commit to harisbal/pandas that referenced this issue Feb 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants