首页 > 解决方案 > Pandas 中与 Groupby 的自相关

问题描述

我正在使用 Python 处理面板数据集。数据框看起来像

stock     date     time   spread  
 VOD      01-01    9:05    0.01          
 VOD      01-01    9:12    0.03        
 VOD      01-01   10:04    0.02          
 VOD      01-01   10:15    0.01        
 VOD      01-01   10:25    0.03       
 VOD      01-01   11:04    0.02     
 VOD      01-02    9:04    0.02     
 ...       ...     ...     ....    
 BAT      01-01    13:05   0.04    
 BAT      01-02    9:07    0.05    
 BAT      01-02    9:10    0.06   

我打算得到价差的股票日自相关表为

stock     date    autocorrelation
 VOD      01-01       0.52          
 VOD      01-02       0.32    
 BAT      01-01       0.23  
 BAT      01-02       0.54
 ...       ...        ... 

我努力了

want=df.groupby(['stock','date'])['spread'].autocorr(lag=1)

但是,系统给了我:

AttributeError: Cannot access callable attribute 'autocorr' of 'SeriesGroupBy' objects, try using the 'apply' method.

看来我不能同时使用Groupbyautocorr功能。

任何人都可以帮忙吗?谢谢!

标签: pandaspandas-groupbyautocorrelation

解决方案


推荐阅读