Skip to content

Commit f33480d

Browse files
authored
CLN: no need to catch libgroupby validation ValueError (#41240)
1 parent 4d604fb commit f33480d

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

pandas/core/resample.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -430,16 +430,23 @@ def _groupby_and_aggregate(self, how, grouper=None, *args, **kwargs):
430430
result = grouped._aggregate_item_by_item(how, *args, **kwargs)
431431
else:
432432
result = grouped.aggregate(how, *args, **kwargs)
433-
except (DataError, AttributeError, KeyError):
433+
except DataError:
434+
# got TypeErrors on aggregation
435+
result = grouped.apply(how, *args, **kwargs)
436+
except (AttributeError, KeyError):
434437
# we have a non-reducing function; try to evaluate
435438
# alternatively we want to evaluate only a column of the input
439+
440+
# test_apply_to_one_column_of_df the function being applied references
441+
# a DataFrame column, but aggregate_item_by_item operates column-wise
442+
# on Series, raising AttributeError or KeyError
443+
# (depending on whether the column lookup uses getattr/__getitem__)
436444
result = grouped.apply(how, *args, **kwargs)
445+
437446
except ValueError as err:
438447
if "Must produce aggregated value" in str(err):
439448
# raised in _aggregate_named
440-
pass
441-
elif "len(index) != len(labels)" in str(err):
442-
# raised in libgroupby validation
449+
# see test_apply_without_aggregation, test_apply_with_mutated_index
443450
pass
444451
else:
445452
raise

pandas/tests/resample/test_resampler_grouper.py

+4
Original file line numberDiff line numberDiff line change
@@ -354,11 +354,15 @@ def test_apply_to_one_column_of_df():
354354
{"col": range(10), "col1": range(10, 20)},
355355
index=date_range("2012-01-01", periods=10, freq="20min"),
356356
)
357+
358+
# access "col" via getattr -> make sure we handle AttributeError
357359
result = df.resample("H").apply(lambda group: group.col.sum())
358360
expected = Series(
359361
[3, 12, 21, 9], index=date_range("2012-01-01", periods=4, freq="H")
360362
)
361363
tm.assert_series_equal(result, expected)
364+
365+
# access "col" via _getitem__ -> make sure we handle KeyErrpr
362366
result = df.resample("H").apply(lambda group: group["col"].sum())
363367
tm.assert_series_equal(result, expected)
364368

0 commit comments

Comments
 (0)