Skip to content

Commit 5498df4

Browse files
Backport PR pandas-dev#37198 on branch 1.1.x (BUG: Regression in Resample.apply raised error when apply affected only a Series) (pandas-dev#37305)
Co-authored-by: patrick <[email protected]>
1 parent df22136 commit 5498df4

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

doc/source/whatsnew/v1.1.4.rst

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Fixed regressions
2020
- Fixed regression in :class:`RollingGroupby` with ``sort=False`` not being respected (:issue:`36889`)
2121
- Fixed regression in :meth:`Series.astype` converting ``None`` to ``"nan"`` when casting to string (:issue:`36904`)
2222
- Fixed regression in :class:`RollingGroupby` causing a segmentation fault with Index of dtype object (:issue:`36727`)
23+
- Fixed regression in :meth:`DataFrame.resample(...).apply(...)` raised ``AttributeError`` when input was a :class:`DataFrame` and only a :class:`Series` was evaluated (:issue:`36951`)
2324
- Fixed regression in :class:`PeriodDtype` comparing both equal and unequal to its string representation (:issue:`37265`)
2425
- Fixed regression where slicing :class:`DatetimeIndex` raised :exc:`AssertionError` on irregular time series with ``pd.NaT`` or on unsorted indices (:issue:`36953` and :issue:`35509`)
2526
- Fixed regression in certain offsets (:meth:`pd.offsets.Day() <pandas.tseries.offsets.Day>` and below) no longer being hashable (:issue:`37267`)

pandas/core/resample.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,9 @@ def _groupby_and_aggregate(self, how, grouper=None, *args, **kwargs):
365365
result = grouped._aggregate_item_by_item(how, *args, **kwargs)
366366
else:
367367
result = grouped.aggregate(how, *args, **kwargs)
368-
except DataError:
368+
except (DataError, AttributeError, KeyError):
369369
# we have a non-reducing function; try to evaluate
370+
# alternatively we want to evaluate only a column of the input
370371
result = grouped.apply(how, *args, **kwargs)
371372
except ValueError as err:
372373
if "Must produce aggregated value" in str(err):

pandas/tests/resample/test_resampler_grouper.py

+15
Original file line numberDiff line numberDiff line change
@@ -346,3 +346,18 @@ def test_median_duplicate_columns():
346346
result = df.resample("5s").median()
347347
expected.columns = result.columns
348348
tm.assert_frame_equal(result, expected)
349+
350+
351+
def test_apply_to_one_column_of_df():
352+
# GH: 36951
353+
df = pd.DataFrame(
354+
{"col": range(10), "col1": range(10, 20)},
355+
index=pd.date_range("2012-01-01", periods=10, freq="20min"),
356+
)
357+
result = df.resample("H").apply(lambda group: group.col.sum())
358+
expected = pd.Series(
359+
[3, 12, 21, 9], index=pd.date_range("2012-01-01", periods=4, freq="H")
360+
)
361+
tm.assert_series_equal(result, expected)
362+
result = df.resample("H").apply(lambda group: group["col"].sum())
363+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)