【问题标题】:Rolling window correlation calculations for different time step in pandaspandas不同时间步长的滚动窗口相关性计算
【发布时间】:2016-02-16 13:36:05
【问题描述】:

我有一个 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 中的所有数据点完成的,而我只需要每个月的最后一天。有什么建议吗?

【问题讨论】:

    标签: python pandas correlation


    【解决方案1】:

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

    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]
    

    有什么东西告诉我这不是你想要的吗?

    【讨论】:

    • 不是。基本上我想做pd.rolling_corr(df2,window=100,pairwise = True) 所做的事情,但是对于 df2 中的所有日期不具有相关矩阵,例如对于月末日期,或者对于 df2 中的每 20 个日期。 IE。这样返回的 DataFrame 将没有 2006-01-24, 2006-01-25, 2006-01-26, 2006-01-27, 2006-01-28, ... 日期,而是 2006-01-31, 2006-02-28, 2006-03-31, 2006-04-30, 2006-05-31,.... I could extract it from the current pd.rolling_corr(df2,window=100,pairwise = True)`结果,但我想减少计算时间。
    猜你喜欢
    • 2019-11-06
    • 1970-01-01
    • 2018-03-27
    • 2018-07-07
    • 2013-01-15
    • 1970-01-01
    • 1970-01-01
    • 2012-12-27
    相关资源
    最近更新 更多