Skip to content

first/last aggregations for DataFrame-/SeriesGroupBy - #3389

Merged
mrocklin merged 2 commits into
dask:masterfrom
almaleksia:first_last_groupby
Apr 12, 2018
Merged

mrocklin merged 2 commits into
dask:masterfrom
almaleksia:first_last_groupby

Conversation

@almaleksia

Copy link
Copy Markdown
Contributor
In [1]: import pandas as pd, dask.dataframe as dd
In [2]: df = pd.DataFrame({'F': list('abcdef'),
                           'B': [4, 5, 4, 5, 5, 4],
                           'C': [7, 8, 9, 4, 2, 3],
                           'D': [1, 3, 5, 7, 1, 0],
                           'E': [5, 3, 6, 9, 2, 4],
                           'A': list('aaabbb')})
In [3]: ddf = dd.from_pandas(df, npartitions=3)
In [4]: ddf.groupby(by='A').agg({'B': 'mean', 'C': 'mean', 'D': 'first', 'E': 'last'}).compute()
Out[4]:
          B    C  D  E
A
a  4.333333  8.0  1  6
b  4.666667  3.0  7  4
In [5]: ddf.groupby(by='A')['D', 'E'].first().compute()
Out[5]:
   D  E
A
a  1  5
b  7  9
In [6]: ddf.groupby(by='A')['D', 'E'].last().compute()
Out[6]:
   D  E
A
a  5  6
b  0  4
  • Tests added / passed
  • Passes flake8 dask
  • Fully documented, including docs/source/changelog.rst for all changes
    and one of the docs/source/*-api.rst files for new API

xref #3206

@mrocklin mrocklin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems great to me. I also really appreciate the effort to clean up the testing infrastructure.

Comment thread dask/dataframe/tests/test_groupby.py Outdated
(ddf.groupby('a')[['b', 'c']], df.groupby('a')[['b', 'c']]),
(ddf.groupby('a')[['b']], df.groupby('a')[['b']]),
(ddf.groupby(['a', 'b', 'c']), df.groupby(['a', 'b', 'c']))]
dask_group, pandas_group = grouper(ddf, df)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this change.

Is there anything stopping us from simplifying the functions to lambda df: df.groupby...) and calling them twice here like

df = grouper(df)
ddf = groupber(ddf)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mrocklin thank you,
actually nothing stops, two arguments here are indeed redundant

```
In [1]: import pandas as pd, dask.dataframe as dd
In [2]: df = pd.DataFrame({'F': list('abcdef'),
                           'B': [4, 5, 4, 5, 5, 4],
                           'C': [7, 8, 9, 4, 2, 3],
                           'D': [1, 3, 5, 7, 1, 0],
                           'E': [5, 3, 6, 9, 2, 4],
                           'A': list('aaabbb')})
In [3]: ddf = dd.from_pandas(df, npartitions=3)
In [4]: ddf.groupby(by='A').agg({'B': 'mean', 'C': 'mean', 'D': 'first', 'E': 'last'}).compute()
Out[4]:
          B    C  D  E
A
a  4.333333  8.0  1  6
b  4.666667  3.0  7  4
In [5]: ddf.groupby(by='A')['D', 'E'].first().compute()
Out[5]:
   D  E
A
a  1  5
b  7  9
In [6]: ddf.groupby(by='A')['D', 'E'].last().compute()
Out[6]:
   D  E
A
a  5  6
b  0  4
```
@mrocklin

Copy link
Copy Markdown
Member

This looks good to me. Merging. My apologies for the delay.

Thank you also for cleaning up the test suite. That is very much appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants