From b81bb4fe57969e4d5dcba0f9fde0e31858b29c9a Mon Sep 17 00:00:00 2001 From: Yuanhao Geng <41546976+GYHHAHA@users.noreply.github.com> Date: Tue, 5 Jul 2022 23:14:05 -0500 Subject: [PATCH 1/7] Update resample.py --- pandas/core/resample.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 0a62861cdaba7..541de2650150e 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -502,11 +502,11 @@ def _apply_loffset(self, result): self.loffset = None return result - def _get_resampler_for_grouping(self, groupby): + def _get_resampler_for_grouping(self, groupby, key=None): """ Return the correct class for resampling with groupby. """ - return self._resampler_for_grouping(self, groupby=groupby) + return self._resampler_for_grouping(self, groupby=groupby, key=key) def _wrap_result(self, result): """ @@ -1132,7 +1132,7 @@ class _GroupByMixin(PandasObject): _attributes: list[str] # in practice the same as Resampler._attributes _selection: IndexLabel | None = None - def __init__(self, obj, parent=None, groupby=None, **kwargs) -> None: + def __init__(self, obj, parent=None, groupby=None, key=None, **kwargs) -> None: # reached via ._gotitem and _get_resampler_for_grouping if parent is None: @@ -1145,6 +1145,7 @@ def __init__(self, obj, parent=None, groupby=None, **kwargs) -> None: self._selection = kwargs.get("selection") self.binner = parent.binner + self.key = None self._groupby = groupby self._groupby.mutated = True @@ -1197,6 +1198,8 @@ def _gotitem(self, key, ndim, subset=None): # Try to select from a DataFrame, falling back to a Series try: + if isinstance(key, list) and self.key not in key: + key.append(self.key) groupby = self._groupby[key] except IndexError: groupby = self._groupby @@ -1511,7 +1514,7 @@ def get_resampler_for_grouping( # .resample uses 'on' similar to how .groupby uses 'key' tg = TimeGrouper(freq=rule, key=on, **kwargs) resampler = tg._get_resampler(groupby.obj, kind=kind) - return resampler._get_resampler_for_grouping(groupby=groupby) + return resampler._get_resampler_for_grouping(groupby=groupby, key=tg.key) class TimeGrouper(Grouper): From 30eaf01965b12cbc7df9d065231b9580d03fa569 Mon Sep 17 00:00:00 2001 From: Yuanhao Geng <41546976+GYHHAHA@users.noreply.github.com> Date: Tue, 5 Jul 2022 23:25:10 -0500 Subject: [PATCH 2/7] Update v1.5.0.rst --- doc/source/whatsnew/v1.5.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 0b450fab53137..24e08f7db7cf8 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -996,6 +996,7 @@ Groupby/resample/rolling - Bug in :meth:`.DataFrameGroupBy.describe` and :meth:`.SeriesGroupBy.describe` produces inconsistent results for empty datasets (:issue:`41575`) - Bug in :meth:`DataFrame.resample` reduction methods when used with ``on`` would attempt to aggregate the provided column (:issue:`47079`) - Bug in :meth:`DataFrame.groupby` and :meth:`Series.groupby` would not respect ``dropna=False`` when the input DataFrame/Series had a NaN values in a :class:`MultiIndex` (:issue:`46783`) +- Bug in :meth:`DataFrameGroupBy.resample` raises ``KeyError`` when getting the result from a key list which misses the resample key (:issue:`47362`) Reshaping ^^^^^^^^^ From 3c8dcbbfcc17b95676fd6bd4e74887fe4ec0b7d6 Mon Sep 17 00:00:00 2001 From: Yuanhao Geng <41546976+GYHHAHA@users.noreply.github.com> Date: Tue, 5 Jul 2022 23:43:07 -0500 Subject: [PATCH 3/7] Update test_resampler_grouper.py --- .../tests/resample/test_resampler_grouper.py | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/pandas/tests/resample/test_resampler_grouper.py b/pandas/tests/resample/test_resampler_grouper.py index c54d9de009940..1b7b601655cbe 100644 --- a/pandas/tests/resample/test_resampler_grouper.py +++ b/pandas/tests/resample/test_resampler_grouper.py @@ -470,3 +470,31 @@ def test_resample_groupby_agg_object_dtype_all_nan(consolidate): index=idx, ) tm.assert_frame_equal(result, expected) + + +def test_groupby_resample_with_list_of_keys(): + # GH 47362 + df = DataFrame( + data={ + "date": date_range(start="2016-01-01", periods=8), + "group": [0, 0, 0, 0, 1, 1, 1, 1], + "val": [1, 7, 5, 2, 3, 10, 5, 1], + + } + ) + result = df.groupby("group").resample("2D", on="date")[["val"]].mean() + expected = DataFrame( + data={ + "val": [4.0, 3.5, 6.5, 3.0], + }, + index=Index( + data=[ + (0, Timestamp("2016-01-01")), + (0, Timestamp("2016-01-03")), + (1, Timestamp("2016-01-05")), + (1, Timestamp("2016-01-07")), + ], + name=("group", "date"), + ) + ) + tm.assert_frame_equal(result, expected) From 6782fa8043a9c4ed51a5176f8304a5cfaec3c892 Mon Sep 17 00:00:00 2001 From: Yuanhao Geng <41546976+GYHHAHA@users.noreply.github.com> Date: Tue, 5 Jul 2022 23:47:22 -0500 Subject: [PATCH 4/7] delete blank --- pandas/tests/resample/test_resampler_grouper.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/tests/resample/test_resampler_grouper.py b/pandas/tests/resample/test_resampler_grouper.py index 1b7b601655cbe..7e0bad0f2799e 100644 --- a/pandas/tests/resample/test_resampler_grouper.py +++ b/pandas/tests/resample/test_resampler_grouper.py @@ -479,7 +479,6 @@ def test_groupby_resample_with_list_of_keys(): "date": date_range(start="2016-01-01", periods=8), "group": [0, 0, 0, 0, 1, 1, 1, 1], "val": [1, 7, 5, 2, 3, 10, 5, 1], - } ) result = df.groupby("group").resample("2D", on="date")[["val"]].mean() From 3d0cded2d625f9cfa268789ac1d6829d6d03f936 Mon Sep 17 00:00:00 2001 From: Yuanhao Geng <41546976+GYHHAHA@users.noreply.github.com> Date: Tue, 5 Jul 2022 23:59:52 -0500 Subject: [PATCH 5/7] Update test_resampler_grouper.py --- pandas/tests/resample/test_resampler_grouper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/resample/test_resampler_grouper.py b/pandas/tests/resample/test_resampler_grouper.py index 7e0bad0f2799e..8aff217cca5c1 100644 --- a/pandas/tests/resample/test_resampler_grouper.py +++ b/pandas/tests/resample/test_resampler_grouper.py @@ -494,6 +494,6 @@ def test_groupby_resample_with_list_of_keys(): (1, Timestamp("2016-01-07")), ], name=("group", "date"), - ) + ), ) tm.assert_frame_equal(result, expected) From c7332fa6e973083cc997d273a5c09441116abde5 Mon Sep 17 00:00:00 2001 From: Yuanhao Geng <41546976+GYHHAHA@users.noreply.github.com> Date: Wed, 6 Jul 2022 00:00:43 -0500 Subject: [PATCH 6/7] Update v1.5.0.rst --- doc/source/whatsnew/v1.5.0.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 24e08f7db7cf8..5b07fc31287e5 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -996,7 +996,8 @@ Groupby/resample/rolling - Bug in :meth:`.DataFrameGroupBy.describe` and :meth:`.SeriesGroupBy.describe` produces inconsistent results for empty datasets (:issue:`41575`) - Bug in :meth:`DataFrame.resample` reduction methods when used with ``on`` would attempt to aggregate the provided column (:issue:`47079`) - Bug in :meth:`DataFrame.groupby` and :meth:`Series.groupby` would not respect ``dropna=False`` when the input DataFrame/Series had a NaN values in a :class:`MultiIndex` (:issue:`46783`) -- Bug in :meth:`DataFrameGroupBy.resample` raises ``KeyError`` when getting the result from a key list which misses the resample key (:issue:`47362`) +- Bug in :meth:`DataFrameGroupBy.resample` raises ``KeyError`` when getting the result from a key list which misses the resample key (:issue:`47362`) +- Reshaping ^^^^^^^^^ From fa6b70a2c89004bf74e9ca483a98c64381cbdb38 Mon Sep 17 00:00:00 2001 From: Yuanhao Geng <41546976+GYHHAHA@users.noreply.github.com> Date: Wed, 6 Jul 2022 00:46:11 -0500 Subject: [PATCH 7/7] Update resample.py --- pandas/core/resample.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 541de2650150e..dc436cd322a8d 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -1145,7 +1145,7 @@ def __init__(self, obj, parent=None, groupby=None, key=None, **kwargs) -> None: self._selection = kwargs.get("selection") self.binner = parent.binner - self.key = None + self.key = key self._groupby = groupby self._groupby.mutated = True