Skip to content

Commit 055651b

Browse files
authored
BUG: Regression in Resample.apply raised error when apply affected only a Series (#37198)
1 parent 951c9c1 commit 055651b

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

2425
.. ---------------------------------------------------------------------------
2526

pandas/core/resample.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,9 @@ def _groupby_and_aggregate(self, how, grouper=None, *args, **kwargs):
369369
result = grouped._aggregate_item_by_item(how, *args, **kwargs)
370370
else:
371371
result = grouped.aggregate(how, *args, **kwargs)
372-
except DataError:
372+
except (DataError, AttributeError, KeyError):
373373
# we have a non-reducing function; try to evaluate
374+
# alternatively we want to evaluate only a column of the input
374375
result = grouped.apply(how, *args, **kwargs)
375376
except ValueError as err:
376377
if "Must produce aggregated value" in str(err):

pandas/tests/resample/test_resampler_grouper.py

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

0 commit comments

Comments
 (0)