大熊猫不同时间步长的滚动窗口相关计算

时间:2016-02-16 13:36:06

标签: python pandas correlation

我有一个DataFrame,可以在几年内获得不同证券的回报。我想在每个月的最后一天计算100天窗口中的相关性。

tobeselected

rolcor = pd.rolling_corr(df2,window=100,pairwise = True) Date Sec1 Sec2 Sec3 Sec4 .... ... 2006-01-24 0.000595 -0.009683 -0.004044 0.020969 .... 2006-01-25 0.013976 0.024152 -0.001015 0.019122 .... 2006-01-26 0.011730 0.008323 0.026423 -0.006254 .... 2006-01-27 0.020290 0.000000 0.014851 0.004196 .... 2006-01-30 0.046875 0.018937 0.000000 0.007660 .... 2006-01-31 -0.049118 -0.014852 -0.006829 -0.005529 .... .... 进行计算,但是我们只对原始DataFrame中的所有数据点进行了计算,而我只需要在每个月的最后一天进行计算。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

据我了解,您只想考虑当月最后一天的价格

periods = n
index = pd.date_range('2006-01-31', periods=n, freq='M')
print index

DatetimeIndex(['2006-01-31', '2006-02-28', ... , '2006-10-31'], dtype='datetime64[ns]', freq='M')

然后使用它来分割出月末值。

df2.loc[index]

有些东西告诉我,这不是你想要的吗?