pythonpandasdataframesplit-apply-combine

pandas groupby shift is not respecting the groups


I have the following DataFrame and an arbitrary function

df = pd.DataFrame(
    {'grp': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3],
     'val': [0.80485036, 0.30698609, 0.33518013, 0.12214516, 0.66355629,
       0.71277808, 0.07193942, 0.97128731, 0.46351423, 0.81494857,
       0.82267912, 0.33043168, 0.55643, 0.63413976, 0.37998928, 0.54695376,
       0.99751999, 0.02726808, 0.2392102 , 0.93278521, 0.41905688]}
)

def myfunc(arr):
    return np.product(1+arr) - 1

I calculate myfunc rolling within groups:

df.groupby('grp')['val'].rolling(3).apply(myfunc)

grp    
1    0          NaN
     1          NaN
     2     2.149576
     3     0.958213
     4     1.492450
     5     2.197331
     6     2.054280
     7     2.619272
     8     2.092553
     9     4.236139
     10    3.841406
2    11         NaN
3    12         NaN
     13         NaN
     14    2.509898
     15    2.488528
     16    3.264265
     17    2.174331
     18    1.542845
     19    1.460438
     20    2.398822

That's all good. Now I need to shift back the rolling calc within the group by five periods.

df.groupby('grp')['val'].rolling(3).apply(myfunc).shift(-5)

grp    
1    0     2.197331
     1     2.054280
     2     2.619272
     3     2.092553
     4     4.236139
     5     3.841406
     6          NaN
     7          NaN
     8          NaN
     9     2.509898
     10    2.488528
2    11    3.264265
3    12    2.174331
     13    1.542845
     14    1.460438
     15    2.398822
     16         NaN
     17         NaN
     18         NaN
     19         NaN
     20         NaN
Name: val, dtype: float64

What is going on here?! The whole purpose of groupby is to maintain boundaries between groups. How (and why) is pandas not respecting that. It should be:

grp    
1    0     2.197331
     1     2.054280
     2     2.619272
     3     2.092553
     4     4.236139
     5     3.841406
     6          NaN
     7          NaN
     8          NaN
     9          NaN
     10         NaN
2    11         NaN
3    12    2.174331
     13    1.542845
     14    1.460438
     15    2.398822
     16         NaN
     17         NaN
     18         NaN
     19         NaN
     20         NaN
Name: val, dtype: float64

This seems like a serious bug in pandas. Am I missing something? How can I make groupby do a groupby?


Solution

  • The problem is, when breaking into pieces, the code

    df.groupby('grp')['val'].rolling(3).apply(myfunc).shift(-5)
    

    is equivalent to

    tmp = df.groupby('grp')['val'].rolling(3).apply(myfunc)
    out = tmp.shift(-5)
    

    Here, tmp is a normal pd.Series. And as you can now guess, out is shifted on a normal series, without any grouping. And this is expected behavior.


    To obtain the desired output, you can chain with another groupby:

    (df.groupby('grp')['val'].rolling(3).apply(myfunc)
       .groupby('grp').shift(-5)        # extra groupby here 
    )
    

    and all should be good.