[英]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.