Skip to content

Exact matches in time-based .rolling() #13965

Closed
@chrisaycock

Description

@chrisaycock

I would like to allow_exact_matches in time-based .rolling(). Using an example similar to the one found in the documentation:

df = pd.DataFrame({'B': [1, 1, 2, np.nan, 4]},
                  index = [pd.Timestamp('20130101 09:00:00'),
                           pd.Timestamp('20130101 09:00:02'),
                           pd.Timestamp('20130101 09:00:03'),
                           pd.Timestamp('20130101 09:00:05'),
                           pd.Timestamp('20130101 09:00:06')])

I can compute the three-second rolling sum:

In [200]: df
Out[200]:
                       B
2013-01-01 09:00:00  1.0
2013-01-01 09:00:02  1.0
2013-01-01 09:00:03  2.0
2013-01-01 09:00:05  NaN
2013-01-01 09:00:06  4.0

In [201]: df.rolling('3s').sum()
Out[201]:
                       B
2013-01-01 09:00:00  1.0
2013-01-01 09:00:02  2.0
2013-01-01 09:00:03  3.0
2013-01-01 09:00:05  2.0
2013-01-01 09:00:06  4.0

But note that 09:00:03 does not include 09:00:00, 09:00:05 does not include 09:00:02, and 09:00:06 does not include 09:00:03. If I were to include these timestamps, then I would have:

                       B
2013-01-01 09:00:00  1.0
2013-01-01 09:00:02  2.0
2013-01-01 09:00:03  4.0
2013-01-01 09:00:05  3.0
2013-01-01 09:00:06  6.0

A quick-and-dirty way of getting these values is with:

from itertools import starmap

window = pd.Timedelta('3s')

begins = np.searchsorted(df.index, df.index - window)
ends = np.arange(len(df)) + 1
ranges = starmap(range, zip(begins, ends))

pd.concat([df.ix[r].sum() for r in ranges])

pd.merge_asof() has the parameter allow_exact_matches to permit this. Would it be possible to add this to .rolling()?

I can try this one myself.

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementResampleresample methodReshapingConcat, Merge/Join, Stack/Unstack, Explode

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions