Skip to content

Commit 883cd51

Browse files
Backport PR pandas-dev#43709: REGR: Passing args/kwargs to Series.agg fails (pandas-dev#43716)
Co-authored-by: Richard Shadrach <[email protected]>
1 parent 5b47a6c commit 883cd51

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

doc/source/whatsnew/v1.3.4.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Fixed regressions
2020
- Fixed regression in :meth:`Series.cat.reorder_categories` failing to update the categories on the ``Series`` (:issue:`43232`)
2121
- Fixed regression in :meth:`Series.cat.categories` setter failing to update the categories on the ``Series`` (:issue:`43334`)
2222
- Fixed regression in :meth:`pandas.read_csv` raising ``UnicodeDecodeError`` exception when ``memory_map=True`` (:issue:`43540`)
23-
-
23+
- Fixed regression in :meth:`Series.aggregate` attempting to pass ``args`` and ``kwargs`` multiple times to the user supplied ``func`` in certain cases (:issue:`43357`)
2424

2525
.. ---------------------------------------------------------------------------
2626
@@ -29,6 +29,7 @@ Fixed regressions
2929
Bug fixes
3030
~~~~~~~~~
3131
- Fixed bug in :meth:`.GroupBy.mean` with datetimelike values including ``NaT`` values returning incorrect results (:issue:`43132`)
32+
- Fixed bug in :meth:`Series.aggregate` not passing the first ``args`` to the user supplied ``func`` in certain cases (:issue:`43357`)
3233

3334
.. ---------------------------------------------------------------------------
3435

pandas/core/apply.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,6 @@ def agg(self):
10461046
result = super().agg()
10471047
if result is None:
10481048
f = self.f
1049-
args = self.args
10501049
kwargs = self.kwargs
10511050

10521051
# string, list-like, and dict-like are entirely handled in super
@@ -1065,9 +1064,9 @@ def agg(self):
10651064
# then .agg and .apply would have different semantics if the
10661065
# operation is actually defined on the Series, e.g. str
10671066
try:
1068-
result = self.obj.apply(f, *args, **kwargs)
1067+
result = self.obj.apply(f)
10691068
except (ValueError, AttributeError, TypeError):
1070-
result = f(self.obj, *args, **kwargs)
1069+
result = f(self.obj)
10711070

10721071
return result
10731072

pandas/tests/apply/test_series_apply.py

+15
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,21 @@ def test_apply_args():
103103
assert isinstance(result[0], list)
104104

105105

106+
@pytest.mark.parametrize(
107+
"args, kwargs, increment",
108+
[((), {}, 0), ((), {"a": 1}, 1), ((2, 3), {}, 32), ((1,), {"c": 2}, 201)],
109+
)
110+
def test_agg_args(args, kwargs, increment):
111+
# GH 43357
112+
def f(x, a=0, b=0, c=0):
113+
return x + a + 10 * b + 100 * c
114+
115+
s = Series([1, 2])
116+
result = s.agg(f, 0, *args, **kwargs)
117+
expected = s + increment
118+
tm.assert_series_equal(result, expected)
119+
120+
106121
def test_series_map_box_timestamps():
107122
# GH#2689, GH#2627
108123
ser = Series(pd.date_range("1/1/2000", periods=10))

0 commit comments

Comments
 (0)