简体   繁体   English

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

[英]Rolling window correlation calculations for different time step in pandas

I have a DataFrame with returns for different securities across a few year. 我有一个DataFrame,其中包含几年内不同证券的收益。 I would like to calculate correlations in 100 day windows for the last day of every month. 我想计算每个月最后一天在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 does the calculations, but they're done for all data points in the original DataFrame while I need only for the last day of each month. pd.rolling_corr了计算,但是对原始DataFrame中的所有数据点都进行了计算,而我只需要每个月的最后一天。 Any suggestions how to do it? 有什么建议怎么办?

As I understand you only want to consider the price at the last day of the month 据我了解,您只想考虑当月最后一天的价格

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')

then use this to slice out the month end values. 然后用它切出月末值。

df2.loc[index]

Something tells me that is not what you want though? 告诉我那不是你想要的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM