Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating Rolling forward averages with pandas

I need to calculate some rolling forward averages in a dataframe and really don't know where to start.

I know if I wanted to select a cell 10 days ahead say I would do df.shift(-10), but what I'm looking to do is calculate the average between 10 and 15 days ahead say.

So what I'm kind of thinking is df.rolling(-10,-15).mean(), if I was trying to calculate just a moving average going backing in time df.rolling(15, 10).mean() would work perfectly and I did think about just calculating the averages like that, and then somehow shifting the data.

Any help would be great

Many thanks

like image 453
top bantz Avatar asked Apr 19 '19 07:04

top bantz


1 Answers

You could calculate the rolling mean 5 days ahead, and then shift that for 10 more periods. Since negative values in rolling are not allowed, you can invert the axis, calculate backwards, and then invert again (see How to use Pandas rolling_* functions on a forward-looking basis):

df = pd.DataFrame(np.random.rand(100, 2))
df[::-1].rolling(5).mean()[::-1].shift(-10)
like image 53
Jondiedoop Avatar answered Sep 25 '22 13:09

Jondiedoop