简体   繁体   中英

Rolling window correlation calculations for different time step in pandas

I have a DataFrame with returns for different securities across a few year. I would like to calculate correlations in 100 day windows for the last day of every month.

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. 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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