Skip to content

ENH: enabled 'on' keyword for groupby(..).resample() #15326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ Other enhancements
- ``Series/DataFrame.asfreq()`` have gained a ``fill_value`` parameter, to fill missing values (:issue:`3715`).
- ``Series/DataFrame.resample.asfreq`` have gained a ``fill_value`` parameter, to fill missing values during resampling (:issue:`3715`).
- ``pandas.tools.hashing`` has gained a ``hash_tuples`` routine, and ``hash_pandas_object`` has gained the ability to hash a ``MultiIndex`` (:issue:`15224`)
- ``groupby(..).resample()`` is now able to use the ``on`` argument. (:issue:`15021`)

.. _ISO 8601 duration: https://en.wikipedia.org/wiki/ISO_8601#Durations

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1140,13 +1140,13 @@ def ohlc(self):

@Substitution(name='groupby')
@Appender(_doc_template)
def resample(self, rule, *args, **kwargs):
def resample(self, rule, on=None, *args, **kwargs):
"""
Provide resampling when using a TimeGrouper
Return a new grouper with our resampler appended
"""
from pandas.tseries.resample import get_resampler_for_grouping
return get_resampler_for_grouping(self, rule, *args, **kwargs)
return get_resampler_for_grouping(self, rule, key=on, *args, **kwargs)

@Substitution(name='groupby')
@Appender(_doc_template)
Expand Down
14 changes: 14 additions & 0 deletions pandas/tseries/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,20 @@ def test_groupby_resample_api(self):
lambda x: x.resample('1D').ffill())[['val']]
assert_frame_equal(result, expected)

def test_grouby_resample_on_api(self):

# GH 15021
# .groupby(...).resample(on=...) results in an unexpected
# keyword warning.
df = pd.DataFrame({'key': ['A', 'B'] * 5,
'dates': pd.date_range('2016-01-01', periods=10),
'values': np.random.randn(10)})

expected = df.set_index('dates').groupby('key').resample('D').mean()

result = df.groupby('key').resample('D', on='dates').mean()
assert_frame_equal(result, expected)

def test_plot_api(self):
tm._skip_if_no_mpl()

Expand Down