From 3a20b0eae1e0c83fd4cb53a8f8f0c2bf4946ed86 Mon Sep 17 00:00:00 2001 From: Chris Roth Date: Wed, 18 Jan 2023 13:58:54 -0600 Subject: [PATCH 1/3] Add failing test reproducing groupby-resample KeyError. --- .../tests/resample/test_resampler_grouper.py | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/pandas/tests/resample/test_resampler_grouper.py b/pandas/tests/resample/test_resampler_grouper.py index 8aff217cca5c1..579e3ac110b13 100644 --- a/pandas/tests/resample/test_resampler_grouper.py +++ b/pandas/tests/resample/test_resampler_grouper.py @@ -497,3 +497,33 @@ def test_groupby_resample_with_list_of_keys(): ), ) tm.assert_frame_equal(result, expected) + + +def test_groupby_resample_on_index_with_list_of_keys(): + # GH 47362 + df = DataFrame( + data={ + "group": [0, 0, 0, 0, 1, 1, 1, 1], + "val": [3, 1, 4, 1, 5, 9, 2, 6], + }, + index=Series( + date_range(start="2016-01-01", periods=8), + name="date", + ), + ) + result = df.groupby("group").resample("2D")[["val"]].mean() + expected = DataFrame( + data={ + "val": [2.0, 2.5, 7.0, 4.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 d4b9b26a2a3052143c0ef72c5b01024c4cb234a4 Mon Sep 17 00:00:00 2001 From: Chris Roth Date: Wed, 18 Jan 2023 14:01:10 -0600 Subject: [PATCH 2/3] Fix groupby-resample KeyError by adding None check. --- 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 0ac43b773bbf9..77fa574a2bd94 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -1238,7 +1238,7 @@ 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: + if isinstance(key, list) and self.key not in key and self.key is not None: key.append(self.key) groupby = self._groupby[key] except IndexError: From fc50e147f6ba6edbaaba84fa5e169a7232be59f4 Mon Sep 17 00:00:00 2001 From: Chris Roth Date: Wed, 18 Jan 2023 14:21:25 -0600 Subject: [PATCH 3/3] Update whatsnew for #50840 --- doc/source/whatsnew/v1.5.3.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.5.3.rst b/doc/source/whatsnew/v1.5.3.rst index 97c4c73f08c37..b59c3a06cdb79 100644 --- a/doc/source/whatsnew/v1.5.3.rst +++ b/doc/source/whatsnew/v1.5.3.rst @@ -34,6 +34,7 @@ Bug fixes - Fixed bug when instantiating a :class:`DataFrame` subclass inheriting from ``typing.Generic`` that triggered a ``UserWarning`` on python 3.11 (:issue:`49649`) - Bug in :func:`pivot_table` with NumPy 1.24 or greater when the :class:`DataFrame` columns has nested elements (:issue:`50342`) - Bug in :func:`pandas.testing.assert_series_equal` (and equivalent ``assert_`` functions) when having nested data and using numpy >= 1.25 (:issue:`50360`) +- Bug in :meth:`DataFrameGroupBy.resample` raises ``KeyError`` when getting the result from a key list when resampling on time index (:issue:`50840`) .. --------------------------------------------------------------------------- .. _whatsnew_153.other: