Skip to content

Commit 6d2293f

Browse files
Nicholas Ver Halenjreback
Nicholas Ver Halen
authored andcommitted
BUG: bug in passing 'on=' keyword for groupby(..).resample()
closes #15021 Author: Nicholas Ver Halen <[email protected]> Closes #15326 from verhalenn/issue15021 and squashes the following commits: 9fc3b4f [Nicholas Ver Halen] Updated the whatsnew for issue 15021 ec1f316 [Nicholas Ver Halen] Created a test for GH 15021 b8b10b0 [Nicholas Ver Halen] Added the on arg to resample on a grouped dataframe.
1 parent 34cdfa4 commit 6d2293f

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

doc/source/whatsnew/v0.20.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ Bug Fixes
434434
- Bug in ``pd.read_csv()`` in which missing data was being improperly handled with ``usecols`` (:issue:`6710`)
435435
- Bug in ``pd.read_csv()`` in which a file containing a row with many columns followed by rows with fewer columns would cause a crash (:issue:`14125`)
436436
- Bug in ``pd.tools.hashing.hash_pandas_object()`` in which hashing of categoricals depended on the ordering of categories, instead of just their values. (:issue:`15143`)
437+
- Bug in ``.groupby(..).resample()`` when passed the ``on=`` kwarg. (:issue:`15021`)
437438

438439
- Bug in ``DataFrame.loc`` with indexing a ``MultiIndex`` with a ``Series`` indexer (:issue:`14730`)
439440

pandas/tseries/resample.py

+4
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,10 @@ def resample(obj, kind=None, **kwds):
975975
def get_resampler_for_grouping(groupby, rule, how=None, fill_method=None,
976976
limit=None, kind=None, **kwargs):
977977
""" return our appropriate resampler when grouping as well """
978+
979+
# .resample uses 'on' similar to how .groupby uses 'key'
980+
kwargs['key'] = kwargs.pop('on', None)
981+
978982
tg = TimeGrouper(freq=rule, **kwargs)
979983
resampler = tg._get_resampler(groupby.obj, kind=kind)
980984
r = resampler._get_resampler_for_grouping(groupby=groupby)

pandas/tseries/tests/test_resample.py

+14
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,20 @@ def test_groupby_resample_api(self):
217217
lambda x: x.resample('1D').ffill())[['val']]
218218
assert_frame_equal(result, expected)
219219

220+
def test_groupby_resample_on_api(self):
221+
222+
# GH 15021
223+
# .groupby(...).resample(on=...) results in an unexpected
224+
# keyword warning.
225+
df = pd.DataFrame({'key': ['A', 'B'] * 5,
226+
'dates': pd.date_range('2016-01-01', periods=10),
227+
'values': np.random.randn(10)})
228+
229+
expected = df.set_index('dates').groupby('key').resample('D').mean()
230+
231+
result = df.groupby('key').resample('D', on='dates').mean()
232+
assert_frame_equal(result, expected)
233+
220234
def test_plot_api(self):
221235
tm._skip_if_no_mpl()
222236

0 commit comments

Comments
 (0)