ENH: enable 'on' keyword for groupby(..).resample() · Issue #15021 · pandas-dev/pandas (original) (raw)
Problem description
resample
gained a new keyword on
to be able to resample based on a column of datetime values instead of the index (which can prevent some use of set_index
/reset_index
). This feature is however not yet available for resample after a groupby.
Code Sample
In [8]: df = pd.DataFrame({'key': ['A', 'B']*5, 'dates': pd.date_range('2016-01-01', periods=10), 'values': np.random.randn(10)})
In [9]: df
Out[9]:
dates key values
0 2016-01-01 A -1.291535
1 2016-01-02 B 0.922945
2 2016-01-03 A -1.039163
3 2016-01-04 B -1.102022
4 2016-01-05 A 1.220347
5 2016-01-06 B 0.265720
6 2016-01-07 A 0.888080
7 2016-01-08 B -0.250236
8 2016-01-09 A 0.485518
9 2016-01-10 B -0.188880
In [11]: df.set_index('dates').groupby('key').resample('D').mean()
Out[11]:
values
key dates
A 2016-01-01 -1.291535
2016-01-02 NaN
2016-01-03 -1.039163
2016-01-04 NaN
2016-01-05 1.220347
2016-01-06 NaN
2016-01-07 0.888080
2016-01-08 NaN
2016-01-09 0.485518
B 2016-01-02 0.922945
2016-01-03 NaN
2016-01-04 -1.102022
2016-01-05 NaN
2016-01-06 0.265720
2016-01-07 NaN
2016-01-08 -0.250236
2016-01-09 NaN
2016-01-10 -0.188880
In [12]: df.groupby('key').resample('D', on='dates').mean()
...
TypeError: __init__() got an unexpected keyword argument 'on'
Using pandas master, 0.19.0+289.g1bf94c8