Skip to content

Commit 342821a

Browse files
jbrockmendelmeeseeksmachine
authored andcommitted
Backport PR pandas-dev#32121: REG: dont call func on empty input
1 parent 6fcc6c3 commit 342821a

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

doc/source/whatsnew/v1.0.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Fixed regressions
2121
- Fixed regression in :meth:`rolling(..).corr() <pandas.core.window.Rolling.corr>` when using a time offset (:issue:`31789`)
2222
- Fixed regression where :func:`read_pickle` raised a ``UnicodeDecodeError`` when reading a py27 pickle with :class:`MultiIndex` column (:issue:`31988`).
2323
- Fixed regression in :class:`DataFrame` arithmetic operations with mis-matched columns (:issue:`31623`)
24+
- Fixed regression in :meth:`GroupBy.agg` calling a user-provided function an extra time on an empty input (:issue:`31760`)
2425
-
2526

2627
.. ---------------------------------------------------------------------------

pandas/core/groupby/groupby.py

+2-9
Original file line numberDiff line numberDiff line change
@@ -923,17 +923,10 @@ def _python_agg_general(self, func, *args, **kwargs):
923923

924924
try:
925925
# if this function is invalid for this dtype, we will ignore it.
926-
func(obj[:0])
926+
result, counts = self.grouper.agg_series(obj, f)
927927
except TypeError:
928928
continue
929-
except AssertionError:
930-
raise
931-
except Exception:
932-
# Our function depends on having a non-empty argument
933-
# See test_groupby_agg_err_catching
934-
pass
935-
936-
result, counts = self.grouper.agg_series(obj, f)
929+
937930
assert result is not None
938931
key = base.OutputKey(label=name, position=idx)
939932
output[key] = self._try_cast(result, obj, numeric_only=True)

pandas/tests/groupby/aggregate/test_aggregate.py

+12
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@
1414
from pandas.core.groupby.grouper import Grouping
1515

1616

17+
def test_groupby_agg_no_extra_calls():
18+
# GH#31760
19+
df = pd.DataFrame({"key": ["a", "b", "c", "c"], "value": [1, 2, 3, 4]})
20+
gb = df.groupby("key")["value"]
21+
22+
def dummy_func(x):
23+
assert len(x) != 0
24+
return x.sum()
25+
26+
gb.agg(dummy_func)
27+
28+
1729
def test_agg_regression1(tsframe):
1830
grouped = tsframe.groupby([lambda x: x.year, lambda x: x.month])
1931
result = grouped.agg(np.mean)

0 commit comments

Comments
 (0)