Adding Timedelta parameter to time-based .rolling() #15440

Closed
chrisaycock opened this Issue Feb 17, 2017 · 6 comments

Comments

Projects
None yet
3 participants
Contributor

chrisaycock commented Feb 17, 2017 edited

Starting with a simple DataFrame:

n = 10
df = pd.DataFrame({'value': np.arange(n)},
                  index=pd.date_range('2015-12-24', periods=n, freq="D"))

I can invoke a time-based .rolling() with a string for the offset:

df.rolling('3d').sum()

I would like to invoke it with a Timedelta since it is easier to arithmetically change the window length:

df.rolling(pd.Timedelta(days=3)).sum()

chrisaycock changed the title from Adding Timedelta parameter to time-based rolling to Adding Timedelta parameter to time-based .rolling() Feb 17, 2017

Contributor

jreback commented Feb 17, 2017

hmm, it allows offsets already (in fact internally that's all its doing), so this must be just an oversight

In [7]: df.rolling(pd.tseries.frequencies.to_offset(pd.Timedelta(days=3))).sum()
Out[7]: 
            value
2015-12-24    0.0
2015-12-25    1.0
2015-12-26    3.0
2015-12-27    6.0
2015-12-28    9.0
2015-12-29   12.0
2015-12-30   15.0
2015-12-31   18.0
2016-01-01   21.0
2016-01-02   24.0

jreback added this to the 0.20.0 milestone Feb 17, 2017

Contributor

chrisaycock commented Feb 17, 2017

Ah, I hadn't known about to_offset()! That's neat.

fjanoos commented Feb 17, 2017

Thanks ! it would be great though if Timedelta were directly supported !

Contributor

jreback commented Feb 17, 2017 edited

@fjanoos we already use to_offset internally

In [1]: from pandas.tseries.frequencies import to_offset

In [2]: to_offset(pd.offsets.Day(1)).nanos
Out[2]: 86400000000000

In [4]: to_offset(pd.Timedelta('1 day')).nanos
Out[4]: 86400000000000

this is just a matter of fixing the checking.

pull-requests are welcomed!

fjanoos commented Mar 5, 2017

Is this change in the master branch now ?

Contributor

jreback commented Mar 5, 2017

yep closed via #15443

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