Skip to content

ENH: Allow time-based rolling window - #2198

Merged
TomAugspurger merged 1 commit into
dask:masterfrom
TomAugspurger:time-window-rolling
Apr 15, 2017
Merged

TomAugspurger merged 1 commit into
dask:masterfrom
TomAugspurger:time-window-rolling

Conversation

@TomAugspurger

Copy link
Copy Markdown
Member

Implements df.rolling('5s') style rolling windows.

In [15]: import dask.dataframe as dd

In [16]: import pandas as pd

In [17]: s = pd.Series(range(10), index=pd.date_range('2017', freq='s', periods=10))

In [18]: dd.from_pandas(s, 2).rolling('2s').count().compute()
Out[18]:
2017-01-01 00:00:00    1.0
2017-01-01 00:00:01    2.0
2017-01-01 00:00:02    2.0
2017-01-01 00:00:03    2.0
2017-01-01 00:00:04    2.0
2017-01-01 00:00:05    2.0
2017-01-01 00:00:06    2.0
2017-01-01 00:00:07    2.0
2017-01-01 00:00:08    2.0
2017-01-01 00:00:09    2.0
dtype: float64

Closes #2190

Just a WIP for now, as I might be going down a rabbit hole. I'm having to add quite a few isinstance(thing, int) and isinstance(thing, timedelta) to handle this, but that might be the best way?

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

Minor comments after a skim

Comment thread dask/dataframe/core.py
raise ValueError('window must be >= 0')
if isinstance(window, int):
if window < 0:
raise ValueError('window must be >= 0')

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.

Is this how Pandas handles things? If the input is an integer then we roll by row number and if the input is a string or offset then we roll by time window?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Essentially yes, with the caveat that if you're using a string, you have to have a DatetimeIndex

Comment thread dask/dataframe/core.py Outdated
window : int or offset
Size of the moving window. This is the number of observations used
for calculating the statistic. The window size must not be so large
as to span more than one adjacent partition.

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.

Recommend increasing this to say that it can either be an integer number of rows or a time offset string, like '10s'. As a casual Pandas user I don't know what an offset is necessarily.

Comment thread dask/dataframe/rolling.py Outdated
prevs = [None] + [(name_a, i) for i in range(df.npartitions - 1)]
elif isinstance(before, datetime.timedelta):
# Assumes monotonic (increasing?) index
f = lambda prev, current, before: prev[prev.index > (current.index.min() - before)]

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.

If possible it would be nice to avoid the lambda. Dynamically generated functions end up being expensive to serialize in a distributed setting. It is common to make small private functions within the same module that are importable (and so trivially pickleable)

Comment thread dask/dataframe/tests/test_rolling.py Outdated
'Rolling [axis=0,center=False,window=4]'}


class TestRollingTimeWindow(object):

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.

FWIW this will be (I think) the first testing class within the codebase. Historically we've used bare functions and pytest fixtures.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Noted :) I'll switch to function-based.

Comment thread dask/dataframe/rolling.py
prevs = [None] + [(name_a, i) for i in range(df.npartitions - 1)]
elif isinstance(before, datetime.timedelta):
# Assumes monotonic (increasing?) index
deltas = pd.Series(df.divisions).diff().iloc[1:-1]

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think that this logic is correct. We're checking to ensure that partition j doesn't request any rows from partition j-2.

For the regular int case, this check happens in overlap_chunk since we take the tail(n) from the previous, and if that isn't length n we know it tried to reach too far back.

@TomAugspurger TomAugspurger changed the title [WIP]ENH: Allow time-based rolling window ENH: Allow time-based rolling window Apr 11, 2017
Comment thread dask/dataframe/tests/test_rolling.py Outdated


@pytest.mark.parametrize('method,args,check_less_precise', [
rolling_method_args_check_less_precies = [

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.

typo with precise?

Comment thread dask/dataframe/tests/test_rolling.py Outdated
'Rolling [center=False,axis=0,window=4000000000]',
'Rolling [center=False,window=4000000000,axis=0]',
'Rolling [axis=0,window=4000000000,center=False]',
'Rolling [axis=0,center=False,window=4000000000]'}

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.

Ooh, should we sort in repr?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I believe pandas' order is consistent. We can follow that.

Comment thread dask/dataframe/rolling.py
for i in range(1, df.npartitions)})
nexts = [(name_b, i) for i in range(1, df.npartitions)] + [None]
elif isinstance(after, datetime.timedelta):
# TODO: Do we have a use-case for this? Pandas doesn't allow negative rolling windows

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 may be the most qualified person to answer this @TomAugspurger :)

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.

If we include this though then we should test it. It looks like things here are uncovered.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It is uncovered. I'll try to write a test that exercises it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

OK, I added some tests for it. I think it's worth keeping, even if it's not applicable for .rolling.

Implements `df.rolling('5s')` style rolling windows.
We expand `map_overlap` to work with either integers or
datetime.timedeltas.

Fixes the order in the Rolling repr to follow pandas' order.
@mrocklin

Copy link
Copy Markdown
Member

+1 from me

@TomAugspurger
TomAugspurger merged commit 727b331 into dask:master Apr 15, 2017
@sinhrks sinhrks added this to the 0.14.2 milestone May 11, 2017
@TomAugspurger
TomAugspurger deleted the time-window-rolling branch September 4, 2017 12:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants