简体   繁体   中英

Pandas Rolling Functions with Groupby

I am having a problem trying to implement the 'rolling' functions in Pandas (ie rolling_std() and rolling_corr()) when using the group by functions. I have tried using the below formulas but I keep getting 'ValueError: cannot reindex from a duplicate axis'.

df is my dataframe:

df.groupby(level='ID')['Val1'].apply(lambda x: pd.rolling_std(x,30))

What I have:

ID  Date    Val1    Val2
A   1-Jan   45      22
A   2-Jan   15      66
A   3-Jan   55      13
B   1-Jan   41      12
B   2-Jan   87      45
B   3-Jan   82      66
C   1-Jan   33      34
C   2-Jan   15      67
C   3-Jan   46      22

What I need:

ID  Date    Val1    Val2    Rolling_Corr    Rolling_Std (Val1)
A   1-Jan   45  22      
A   2-Jan   15  66  0.1 1.2
A   3-Jan   55  13  0.16    2.5
B   1-Jan   41  12      
B   2-Jan   87  45  0.15    2.8
B   3-Jan   82  66  0.05    1.1
C   1-Jan   33  34      
C   2-Jan   15  67  0.09    1.5
C   3-Jan   46  22  0.11    2.4

Thanks

In newer versions of pandas the syntax of rolling has changed, for example, from rolling_std() to rolling().std() and works well when combined with groupby :

df.groupby('ID').rolling(2).std()

     ID   Date       Val1       Val2
ID                                  
A  0  A  1-Jan        NaN        NaN
   1  A  2-Jan  21.213203  31.112698
   2  A  3-Jan  28.284271  37.476659
B  3  B  1-Jan        NaN        NaN
   4  B  2-Jan  32.526912  23.334524
   5  B  3-Jan   3.535534  14.849242
C  6  C  1-Jan        NaN        NaN
   7  C  2-Jan  12.727922  23.334524
   8  C  3-Jan  21.920310  31.819805

Or see the following for corr which is similar but slightly more complicated due to the verbose output of corr :

Rolling Correlation with Groupby in Pandas

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