Skip to content

BUG: Frequency-based shifting doesn't work with groupby and MultiIndex #11452

Closed
@naught101

Description

@naught101

If you have a dataframe with a multiindex, and use groupby and then shift, using the frequency argument, the shifting fails, resulting in an empty result. For example:

import numpy as np
import pandas as pd

dates1 = pd.date_range('2015-10-01', '2015-10-03')
df1 = pd.DataFrame(dict(grp='A', blah=[1,2,3]), index=dates1) .set_index('grp', append=True)

dates2 = pd.date_range('2015-10-02', '2015-10-04')
df2 = pd.DataFrame(dict(grp='B', blah=[10,np.nan,30]), index=dates2) .set_index('grp', append=True)

df = pd.concat([df1, df2])

In [ ]: df
Out[ ]: 
                blah
           grp      
2015-10-01 A       1
2015-10-02 A       2
2015-10-03 A       3
2015-10-02 B      10
2015-10-03 B     NaN
2015-10-04 B      30

# This works:
In [ ]: df.groupby(level='grp').shift(1)
Out[ ]: 
                blah
           grp      
2015-10-01 A     NaN
2015-10-02 A       1
2015-10-03 A       2
2015-10-02 B     NaN
2015-10-03 B      10
2015-10-04 B     NaN

# This fails:
In [ ]: df.groupby(level='grp').shift(1, freq='D')
Out[ ]: 
Empty DataFrame
Columns: []
Index: []

I'm not sure why, however using 'grp' as a column, it works fine:

In [ ]: df.reset_index('grp').groupby('grp').shift(1, freq='D')
Out[ ]: 
                blah
grp                 
A   2015-10-02     1
    2015-10-03     2
    2015-10-04     3
B   2015-10-03    10
    2015-10-04   NaN
    2015-10-05    30

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions