From b8b10b02ac2123f9d5d6f1cb1b1c1d7c30855e22 Mon Sep 17 00:00:00 2001 From: Nicholas Ver Halen Date: Mon, 6 Feb 2017 18:30:34 -0600 Subject: [PATCH 1/3] Added the on arg to resample on a grouped dataframe. --- pandas/core/groupby.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/groupby.py b/pandas/core/groupby.py index 99220232114ce..9cdbcf5d07402 100644 --- a/pandas/core/groupby.py +++ b/pandas/core/groupby.py @@ -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) From ec1f316302a4ae2094200aedeb53e386fe3dc1a6 Mon Sep 17 00:00:00 2001 From: Nicholas Ver Halen Date: Mon, 6 Feb 2017 19:56:43 -0600 Subject: [PATCH 2/3] Created a test for GH 15021 --- pandas/tseries/tests/test_resample.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pandas/tseries/tests/test_resample.py b/pandas/tseries/tests/test_resample.py index 56953541265a6..193dec44c0d7e 100755 --- a/pandas/tseries/tests/test_resample.py +++ b/pandas/tseries/tests/test_resample.py @@ -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() From 9fc3b4fa45112c1eb69197bb4d9c553546ea5a78 Mon Sep 17 00:00:00 2001 From: Nicholas Ver Halen Date: Mon, 6 Feb 2017 20:48:42 -0600 Subject: [PATCH 3/3] Updated the whatsnew for issue 15021 --- doc/source/whatsnew/v0.20.0.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v0.20.0.txt b/doc/source/whatsnew/v0.20.0.txt index 1a32498d53c23..1c00c18a24717 100644 --- a/doc/source/whatsnew/v0.20.0.txt +++ b/doc/source/whatsnew/v0.20.0.txt @@ -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