[英]Rolling window correlation calculations for different time step in pandas
我有一个DataFrame,其中包含几年内不同证券的收益。 我想计算每个月最后一天在100天窗口中的相关性。
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 ....
....
pd.rolling_corr
了计算,但是对原始DataFrame中的所有数据点都进行了计算,而我只需要每个月的最后一天。 有什么建议怎么办?
据我了解,您只想考虑当月最后一天的价格
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]
告诉我那不是你想要的吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.