Skip to content

Commit 836139e

Browse files
committed
Fixed bug in df.resample using 'on' parameter
1 parent f483321 commit 836139e

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

doc/source/whatsnew/v0.23.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ Groupby/Resample/Rolling
515515
- Bug in :func:`DataFrame.resample` which silently ignored unsupported (or mistyped) options for ``label``, ``closed`` and ``convention`` (:issue:`19303`)
516516
- Bug in :func:`DataFrame.groupby` where tuples were interpreted as lists of keys rather than as keys (:issue:`17979`, :issue:`18249`)
517517
- Bug in ``transform`` where particular aggregation functions were being incorrectly cast to match the dtype(s) of the grouped data (:issue:`19200`)
518-
-
518+
- Bug in `DataFrame.groupby` with resample using `on` parameter when selecting column to apply function (:issue:`17813`)
519519

520520
Sparse
521521
^^^^^^

pandas/core/groupby.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ def _set_grouper(self, obj, sort=False):
466466
"The Grouper cannot specify both a key and a level!")
467467

468468
# the key must be a valid info item
469-
if self.key is not None:
469+
if self.key is not None and not isinstance(obj, Series):
470470
key = self.key
471471
if key not in obj._info_axis:
472472
raise KeyError("The grouper name {0} is not found".format(key))

pandas/core/resample.py

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
_pipe_template)
1313

1414
from pandas.tseries.frequencies import to_offset, is_subperiod, is_superperiod
15+
from pandas.core.index import Index
1516
from pandas.core.indexes.datetimes import DatetimeIndex, date_range
1617
from pandas.core.indexes.timedeltas import TimedeltaIndex
1718
from pandas.tseries.offsets import DateOffset, Tick, Day, delta_to_nanoseconds
@@ -742,6 +743,11 @@ def func(x):
742743

743744
return x.apply(f, **kwargs)
744745

746+
# Patch the index in case we're applying after __getitem__. GH 17813
747+
if self.groupby.key:
748+
key = self.groupby.key
749+
self._groupby.obj.index = Index(self.groupby.obj[key], key=key)
750+
745751
result = self._groupby.apply(func)
746752
return self._wrap_result(result)
747753

pandas/tests/test_resample.py

+9
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,15 @@ def test_pipe(self):
252252
result = r.pipe(lambda x: x.max() - x.mean())
253253
tm.assert_frame_equal(result, expected)
254254

255+
def test_groupby_resample_on_api_with_getitem(self):
256+
# GH 17813
257+
df = pd.DataFrame({'id': list('aabbb'),
258+
'date': pd.date_range('1-1-2016', periods=5),
259+
'data': 1})
260+
exp = df.set_index('date').groupby('id').resample('2D')['data'].sum()
261+
result = df.groupby('id').resample('2D', on='date')['data'].sum()
262+
assert_series_equal(result, exp)
263+
255264
@td.skip_if_no_mpl
256265
def test_plot_api(self):
257266
# .resample(....).plot(...)

0 commit comments

Comments
 (0)