Racing Tadpole
Racing Tadpole

Reputation: 4370

How do I calculate a rolling mean with custom weights in pandas?

The Pandas documentation http://pandas.pydata.org/pandas-docs/stable/computation.html has an example of how to calculate moving averages:

ser = pd.Series(np.random.randn(10), index=pd.date_range('1/1/2000', periods=10))
pd.rolling_window(ser, 5, 'boxcar')

The second line calculates a rolling average with a window of 5 and equal weights on each of the five observations. The docs refer tantalizingly to the possibility of using custom weights ("When passing a win_type instead of explicitly specifying the weights..."), but how do you do it?

Thanks!

Upvotes: 2

Views: 3607

Answers (2)

Leonid Mednikov
Leonid Mednikov

Reputation: 973

First of all in new pandas versions rolling syntaxis had been changed If you need just triangle window, you can do it like this:

ser.rolling(5, win_type='triang').mean()

Some other standard windows are also supported

If you want really custom (self made) average weights, you can use custom .apply and weighted average np.average like this

import numpy as np

# your code here

weights = [1, 2, 8, 3, 2]  # something unusual
ser.rolling(5).apply(lambda seq: np.average(seq, weights=weights))

Upvotes: 0

jezrael
jezrael

Reputation: 863801

I'm not Math expert, but stahlous explain what you need here.

I try test it:

import pandas as pd

ser = pd.Series([1,1,1], index=pd.date_range('1/1/2000', periods=3))
print ser

rm1 = pd.rolling_window(ser, window=[2,2,2], mean=False)
rm2 = pd.rolling_window(ser, window=[2,2,2]) #, mean=True

print rm1
#
#2000-01-01   NaN
#2000-01-02   NaN
#2000-01-03     6
#Freq: D, dtype: float64
print rm2
#
#2000-01-01   NaN
#2000-01-02   NaN
#2000-01-03     1
#Freq: D, dtype: float64

I setting window to ndarray ([2,2,2]) and calculated weighted sum (rm1) and weighted mean (rm2).

pandas.rolling_window:

window : int or ndarray:
Weighting window specification. If the window is an integer, then it is treated as the window length and win_type is required

mean : boolean, default True
If True computes weighted mean, else weighted sum

Upvotes: 1

Related Questions