Skip to content

resample.agg - #3202

Merged
mrocklin merged 10 commits into
dask:masterfrom
postelrich:resample-agg
Feb 27, 2018
Merged

mrocklin merged 10 commits into
dask:masterfrom
postelrich:resample-agg

Conversation

@postelrich

@postelrich postelrich commented Feb 23, 2018 •

Copy link
Copy Markdown
Contributor

This PR adds in resample.agg that allows for something like df['col'].resample('1min').agg(['mean', 'min']). Seems like a trivial add but useful feature so adding it.

  • 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

@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.

I'm glad to see changes like this. I suspect that @TomAugspurger might want to take a look.

It would be useful to add a test or two first though to ensure correctness into the future.

Comment thread dask/dataframe/tseries/resample.py Outdated
return Series(dsk, name, meta, outdivs)

@derived_from(pd_Resampler)
def agg(self, *args):

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 notice that the pandas version of this also supports **kwargs. Should we do the same here?

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.

Yeah. These keywords are passed through to the aggfunc, so .agg('std', ddof=0)

Comment thread docs/source/changelog.rst

Core
++++

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.

Thank you for setting this up! My bad on omitting it when doing the last release.

Comment thread docs/source/changelog.rst
DataFrame
+++++++++

- Add ``dd.tseries.Resampler.agg`` (:pr:`3202`) `Richard Postelnik`_

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.

You'll also want to add your name and a website to the bottom of this document.

Comment thread dask/dataframe/tseries/resample.py Outdated
self._kwargs = kwargs

def _agg(self, how, meta=None, fill_value=np.nan):
def _agg(self, how, meta=None, fill_value=np.nan, how_args=[]):

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.

Mind changing this to a tuple so that it's immutable? Just in case the function is refactored in the future.

Comment thread dask/dataframe/tseries/resample.py Outdated
return Series(dsk, name, meta, outdivs)

@derived_from(pd_Resampler)
def agg(self, *args):

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.

Yeah. These keywords are passed through to the aggfunc, so .agg('std', ddof=0)

@postelrich

Copy link
Copy Markdown
Contributor Author

Ok made the changes, @TomAugspurger need some advice for the kwargs test, see comment.

ds = dd.from_pandas(ps, npartitions=2)
def foo(series, *args, bar=1):
return [bar] * len(series)
res = ds.resample('10min').agg(foo, bar=2)

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.

@TomAugspurger can't get this mock test to work. Not sure the correct syntax for an aggregate function. Tried a variety of signatures.

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 would avoid functions that return sequences. Does it work if you just apply a function like `len(x) * bar

And does it fail on the dask side or the pandas side?

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.

I get a dask error if i try to compute:

TypeError: foo() missing 1 required positional argument: 'series'

The odd part is that it gets past the step where it tries to determine the meta here: https://github.com/dask/dask/pull/3202/files#diff-394a2aeb5f331b788b4adb6242f9091aR125

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 is because having the tuple (foo,) in the task graph gets evaluated as foo(). I've changed this to [foo] and pushed.

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've also screwed with the test a bit. Things fail around the midpoint of the resample.

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 thanks, with your fix and choosing a proper sample freq the test passes.

mrocklin and others added 2 commits February 26, 2018 13:27
This gets evaluated eagerly.  Convert to list instead.

Also a couple of minor style changes.
@mrocklin

Copy link
Copy Markdown
Member

Note, this fails with syntax errors in Python 2. Check travis-ci.

@postelrich

Copy link
Copy Markdown
Contributor Author

@mrocklin fixed tests

@TomAugspurger TomAugspurger 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.

Looks good to me. Will merge tomorrow unless @mrocklin has any further thoughts.

@mrocklin
mrocklin merged commit 7e0bf9b into dask:master Feb 27, 2018
@mrocklin

Copy link
Copy Markdown
Member

Merged. Thanks for your work here @postelrich !

@postelrich
postelrich deleted the resample-agg branch February 27, 2018 15:31
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.

3 participants