我希望能够根据数据框中的部分数据计算滚动标准偏差。一个例子将解释我想要完成的任务。
A B C
2000-01-01 0.425615 1.679789 -1.903056
2000-01-02 0.791313 0.562471 0.098124
2000-01-03 1.223165 -0.548387 -1.558204
2000-01-04 0.354931 -0.685773 0.647817
2000-01-05 1.137434 1.000594 0.428180
2000-01-06 -0.265311 -1.807045 0.533477
2000-01-07 0.717505 1.647540 -0.141123
2000-01-08 -2.405664 1.278410 1.043872
2000-01-09 0.463943 0.982042 -0.382241
2000-01-10 -0.403267 -0.615421 0.583384
2000-01-11 -0.714163 0.470505 -0.291396
2000-01-12 0.209979 -0.118331 -0.369776
2000-01-13 -0.779638 0.924612 -0.477497
2000-01-14 0.149868 -0.376292 0.747637
2000-01-15 -0.464360 0.821400 1.412874
这是我希望能够做到的:
答案 0 :(得分:3)
window_step_size = 3
rolling_window = 3
>>> pd.rolling_std(df.ix[df.index[::-1][::window_step_size][::-1]], window=rolling_window)
A B C
2000-01-03 NaN NaN NaN
2000-01-06 NaN NaN NaN
2000-01-09 0.744288 1.396749 1.048535
2000-01-12 0.370182 1.404848 0.525129
2000-01-15 0.479753 0.594379 1.032831
df.index[::-1]
会撤消索引中的日期,以便最近的日期是第一个。 df.ix[df.index[::-1][::window_step_size]
然后从此索引中获取每个nth
值(例如,每隔三个日期)。最后,df.index [:: - 1] [:: window_step_size] [:: - 1]首先使用最早的日期对索引进行调整。
>>> df.index[::-1][::window_step_size][::-1]
Index([u'2000-01-03', u'2000-01-06', u'2000-01-09', u'2000-01-12', u'2000-01-15'], dtype='object')
根据此新索引,从数据库中选择值:
>>> df.ix[df.index[::-1][::window_step_size][::-1]]
A B C
2000-01-03 1.223165 -0.548387 -1.558204
2000-01-06 -0.265311 -1.807045 0.533477
2000-01-09 0.463943 0.982042 -0.382241
2000-01-12 0.209979 -0.118331 -0.369776
2000-01-15 -0.464360 0.821400 1.412874
现在,您可以在所选的滚动窗口中使用常规pd.rolling_std
函数。
pd.rolling_std(df.ix[df.index[::-1][::window_step_size][::-1]], window=rolling_window)
编辑要获取每日值,您可以连接。
def roll_sd(df, rolling_window, window_step_size):
return pd.rolling_std(df.ix[df.index[::-1][::window_step_size][::-1]],
window=rolling_window)
df_sd = pd.concat([roll_sd(df.iloc[0:len(df)-n], rolling_window, window_step_size)
for n in range(window_step_size)])
df_sd.sort_index()
A B C
2000-01-01 NaN NaN NaN
2000-01-02 NaN NaN NaN
2000-01-03 NaN NaN NaN
2000-01-04 NaN NaN NaN
2000-01-05 NaN NaN NaN
2000-01-06 NaN NaN NaN
2000-01-07 0.192205 1.356544 1.305998
2000-01-08 1.953373 0.360948 0.480009
2000-01-09 0.744288 1.396749 1.048535
2000-01-10 0.571905 1.327296 0.438081
2000-01-11 1.772152 0.410464 0.668307
2000-01-12 0.370182 1.404848 0.525129
2000-01-13 0.778805 1.155806 0.542145
2000-01-14 1.299902 0.827427 0.701223
2000-01-15 0.479753 0.594379 1.032831