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

左心房为你撑大大i 提交于 2019-12-02 04:29:22

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!