From eae304ef360d65c62be4ef21a44f5b052a60d843 Mon Sep 17 00:00:00 2001 From: phofl Date: Sat, 17 Oct 2020 14:51:27 +0200 Subject: [PATCH 1/3] BUG: Regression in Resample.apply raised error when apply affected only a Series --- doc/source/whatsnew/v1.1.4.rst | 1 + pandas/core/resample.py | 3 ++- pandas/tests/resample/test_resampler_grouper.py | 13 +++++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.4.rst b/doc/source/whatsnew/v1.1.4.rst index 6892fb62028c9..6e365e2fff15c 100644 --- a/doc/source/whatsnew/v1.1.4.rst +++ b/doc/source/whatsnew/v1.1.4.rst @@ -20,6 +20,7 @@ Fixed regressions - Fixed regression in :class:`RollingGroupby` with ``sort=False`` not being respected (:issue:`36889`) - Fixed regression in :meth:`Series.astype` converting ``None`` to ``"nan"`` when casting to string (:issue:`36904`) - Fixed regression in :class:`RollingGroupby` causing a segmentation fault with Index of dtype object (:issue:`36727`) +- Fixed regression in :meth:`Resample.apply` raised ``AttributeError`` when input was a :class:`DataFrame` and only a :class:`Series` was evaluated (:issue:`36951`) .. --------------------------------------------------------------------------- diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 3f1b1dac080a7..76accdb240653 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -369,8 +369,9 @@ def _groupby_and_aggregate(self, how, grouper=None, *args, **kwargs): result = grouped._aggregate_item_by_item(how, *args, **kwargs) else: result = grouped.aggregate(how, *args, **kwargs) - except DataError: + except (DataError, AttributeError): # we have a non-reducing function; try to evaluate + # alternatively we want to evaluate only a column of the input result = grouped.apply(how, *args, **kwargs) except ValueError as err: if "Must produce aggregated value" in str(err): diff --git a/pandas/tests/resample/test_resampler_grouper.py b/pandas/tests/resample/test_resampler_grouper.py index 73bf7dafac254..5c7f2ce68212e 100644 --- a/pandas/tests/resample/test_resampler_grouper.py +++ b/pandas/tests/resample/test_resampler_grouper.py @@ -347,3 +347,16 @@ def test_median_duplicate_columns(): result = df.resample("5s").median() expected.columns = result.columns tm.assert_frame_equal(result, expected) + + +def test_apply_to_one_column_of_df(): + # GH: 36951 + df = pd.DataFrame( + {"col": range(10), "col1": range(10, 20)}, + index=pd.date_range("2012-01-01", periods=10, freq="20min"), + ) + result = df.resample("H").apply(lambda group: group.col.sum()) + expected = pd.Series( + [3, 12, 21, 9], index=pd.date_range("2012-01-01", periods=4, freq="H") + ) + tm.assert_series_equal(result, expected) From 9d3e64c3897f427ba62857720dd0ca8f31aa1f37 Mon Sep 17 00:00:00 2001 From: phofl Date: Sat, 17 Oct 2020 18:41:30 +0200 Subject: [PATCH 2/3] Fix whatsnew --- doc/source/whatsnew/v1.1.4.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.4.rst b/doc/source/whatsnew/v1.1.4.rst index 6e365e2fff15c..1abaf28e14af0 100644 --- a/doc/source/whatsnew/v1.1.4.rst +++ b/doc/source/whatsnew/v1.1.4.rst @@ -20,7 +20,7 @@ Fixed regressions - Fixed regression in :class:`RollingGroupby` with ``sort=False`` not being respected (:issue:`36889`) - Fixed regression in :meth:`Series.astype` converting ``None`` to ``"nan"`` when casting to string (:issue:`36904`) - Fixed regression in :class:`RollingGroupby` causing a segmentation fault with Index of dtype object (:issue:`36727`) -- Fixed regression in :meth:`Resample.apply` raised ``AttributeError`` when input was a :class:`DataFrame` and only a :class:`Series` was evaluated (:issue:`36951`) +- Fixed regression in :meth:`DataFrame.resample(...).apply(...)` raised ``AttributeError`` when input was a :class:`DataFrame` and only a :class:`Series` was evaluated (:issue:`36951`) .. --------------------------------------------------------------------------- From 03824e60f3e6a4d24b9daa1dcfa8dd119f6a1167 Mon Sep 17 00:00:00 2001 From: phofl Date: Sat, 17 Oct 2020 18:43:07 +0200 Subject: [PATCH 3/3] Add test and catch KeyError --- pandas/core/resample.py | 2 +- pandas/tests/resample/test_resampler_grouper.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 76accdb240653..caef938272f6f 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -369,7 +369,7 @@ def _groupby_and_aggregate(self, how, grouper=None, *args, **kwargs): result = grouped._aggregate_item_by_item(how, *args, **kwargs) else: result = grouped.aggregate(how, *args, **kwargs) - except (DataError, AttributeError): + except (DataError, AttributeError, KeyError): # we have a non-reducing function; try to evaluate # alternatively we want to evaluate only a column of the input result = grouped.apply(how, *args, **kwargs) diff --git a/pandas/tests/resample/test_resampler_grouper.py b/pandas/tests/resample/test_resampler_grouper.py index 5c7f2ce68212e..2f22be7c8cce9 100644 --- a/pandas/tests/resample/test_resampler_grouper.py +++ b/pandas/tests/resample/test_resampler_grouper.py @@ -360,3 +360,5 @@ def test_apply_to_one_column_of_df(): [3, 12, 21, 9], index=pd.date_range("2012-01-01", periods=4, freq="H") ) tm.assert_series_equal(result, expected) + result = df.resample("H").apply(lambda group: group["col"].sum()) + tm.assert_series_equal(result, expected)