Repository navigation
ENH: Allow time-based rolling window - #2198
Conversation
| raise ValueError('window must be >= 0') | ||
| if isinstance(window, int): | ||
| if window < 0: | ||
| raise ValueError('window must be >= 0') |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Essentially yes, with the caveat that if you're using a string, you have to have a DatetimeIndex
| 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. |
There was a problem hiding this comment.
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.
| 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)] |
There was a problem hiding this comment.
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)
| 'Rolling [axis=0,center=False,window=4]'} | ||
|
|
||
|
|
||
| class TestRollingTimeWindow(object): |
There was a problem hiding this comment.
FWIW this will be (I think) the first testing class within the codebase. Historically we've used bare functions and pytest fixtures.
There was a problem hiding this comment.
Noted :) I'll switch to function-based.
2d1b187 to
3c01ab2
Compare
| 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] |
There was a problem hiding this comment.
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.
|
|
||
|
|
||
| @pytest.mark.parametrize('method,args,check_less_precise', [ | ||
| rolling_method_args_check_less_precies = [ |
| '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]'} |
There was a problem hiding this comment.
I believe pandas' order is consistent. We can follow that.
| 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 |
There was a problem hiding this comment.
You may be the most qualified person to answer this @TomAugspurger :)
There was a problem hiding this comment.
If we include this though then we should test it. It looks like things here are uncovered.
There was a problem hiding this comment.
It is uncovered. I'll try to write a test that exercises it.
There was a problem hiding this comment.
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.
dda3a36 to
b79fb5e
Compare
|
+1 from me |
Implements
df.rolling('5s')style rolling windows.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)andisinstance(thing, timedelta)to handle this, but that might be the best way?