From b612057f044e1359e8a365b433fb73b4e3522e54 Mon Sep 17 00:00:00 2001 From: Richard Shadrach Date: Sun, 5 Sep 2021 15:15:01 -0400 Subject: [PATCH 1/4] WIP --- pandas/core/apply.py | 6 +++--- pandas/tests/apply/test_series_apply.py | 13 +++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/pandas/core/apply.py b/pandas/core/apply.py index bb39e18caeaa2..42d60deea70fa 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -1044,8 +1044,8 @@ def apply(self) -> DataFrame | Series: def agg(self): result = super().agg() if result is None: + # super().agg() handles list/dict-likes, so args and kwargs already curried f = self.f - args = self.args kwargs = self.kwargs # string, list-like, and dict-like are entirely handled in super @@ -1064,9 +1064,9 @@ def agg(self): # then .agg and .apply would have different semantics if the # operation is actually defined on the Series, e.g. str try: - result = self.obj.apply(f, *args, **kwargs) + result = self.obj.apply(f) except (ValueError, AttributeError, TypeError): - result = f(self.obj, *args, **kwargs) + result = f(self.obj) return result diff --git a/pandas/tests/apply/test_series_apply.py b/pandas/tests/apply/test_series_apply.py index 2af340f0c1bb9..15468e8e123b3 100644 --- a/pandas/tests/apply/test_series_apply.py +++ b/pandas/tests/apply/test_series_apply.py @@ -92,6 +92,19 @@ def test_apply_args(): assert isinstance(result[0], list) +@pytest.mark.parametrize( + "args, kwargs", [(tuple(), {}), (tuple(), {"a": 1}), ((1,), {"c": 2})] +) +def test_agg_args(args, kwargs): + def f(x, a=0, b=0, c=0): + return x + a + b + c + + s = Series([1, 2]) + result = s.agg(f, 0, *args, **kwargs) + expected = s + sum(args) + sum(kwargs.values()) + tm.assert_series_equal(result, expected) + + def test_series_map_box_timestamps(): # GH#2689, GH#2627 ser = Series(pd.date_range("1/1/2000", periods=10)) From 59a466a2750d96b4d51043e165c91556bc712d93 Mon Sep 17 00:00:00 2001 From: Richard Shadrach Date: Wed, 22 Sep 2021 22:57:47 -0400 Subject: [PATCH 2/4] Fixup, improve test --- pandas/core/apply.py | 1 - pandas/tests/apply/test_series_apply.py | 9 +++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/core/apply.py b/pandas/core/apply.py index ffc6ffceb4a11..dafcc71ae0b5a 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -1050,7 +1050,6 @@ def apply(self) -> DataFrame | Series: def agg(self): result = super().agg() if result is None: - # super().agg() handles list/dict-likes, so args and kwargs already curried f = self.f kwargs = self.kwargs diff --git a/pandas/tests/apply/test_series_apply.py b/pandas/tests/apply/test_series_apply.py index 15468e8e123b3..57c5aa0c8964c 100644 --- a/pandas/tests/apply/test_series_apply.py +++ b/pandas/tests/apply/test_series_apply.py @@ -93,15 +93,16 @@ def test_apply_args(): @pytest.mark.parametrize( - "args, kwargs", [(tuple(), {}), (tuple(), {"a": 1}), ((1,), {"c": 2})] + "args, kwargs, increment", + [((), {}, 0), ((), {"a": 1}, 1), ((2, 3), {}, 32), ((1,), {"c": 2}, 201)], ) -def test_agg_args(args, kwargs): +def test_agg_args(args, kwargs, increment): def f(x, a=0, b=0, c=0): - return x + a + b + c + return x + a + 10 * b + 100 * c s = Series([1, 2]) result = s.agg(f, 0, *args, **kwargs) - expected = s + sum(args) + sum(kwargs.values()) + expected = s + increment tm.assert_series_equal(result, expected) From 056015ac711a9a8300d4f5b6173f74ed926361a8 Mon Sep 17 00:00:00 2001 From: Richard Shadrach Date: Wed, 22 Sep 2021 23:04:46 -0400 Subject: [PATCH 3/4] whatsnew --- doc/source/whatsnew/v1.3.4.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.3.4.rst b/doc/source/whatsnew/v1.3.4.rst index 6212f2c6f3399..caec32ed3342b 100644 --- a/doc/source/whatsnew/v1.3.4.rst +++ b/doc/source/whatsnew/v1.3.4.rst @@ -20,7 +20,7 @@ Fixed regressions - Fixed regression in :meth:`Series.cat.reorder_categories` failing to update the categories on the ``Series`` (:issue:`43232`) - Fixed regression in :meth:`Series.cat.categories` setter failing to update the categories on the ``Series`` (:issue:`43334`) - Fixed regression in :meth:`pandas.read_csv` raising ``UnicodeDecodeError`` exception when ``memory_map=True`` (:issue:`43540`) -- +- Fixed regression in :meth:`Series.aggregate` attempting to pass ``args`` and ``kwargs`` multiple times to the user supplied ``func`` in certain cases (:issue:`43357`) .. --------------------------------------------------------------------------- @@ -29,6 +29,7 @@ Fixed regressions Bug fixes ~~~~~~~~~ - Fixed bug in :meth:`.GroupBy.mean` with datetimelike values including ``NaT`` values returning incorrect results (:issue:`43132`) +- Fixed bug in :meth:`Series.aggregate` not passing the first ``args`` to the user supplied ``func`` in certain cases (:issue:`43357`) .. --------------------------------------------------------------------------- From e00823038cb5b0fbbf6d988a7d5c0857f639a5c3 Mon Sep 17 00:00:00 2001 From: Richard Shadrach Date: Wed, 22 Sep 2021 23:13:21 -0400 Subject: [PATCH 4/4] GH # --- pandas/tests/apply/test_series_apply.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/apply/test_series_apply.py b/pandas/tests/apply/test_series_apply.py index 57c5aa0c8964c..2d0202fbeb6c8 100644 --- a/pandas/tests/apply/test_series_apply.py +++ b/pandas/tests/apply/test_series_apply.py @@ -97,6 +97,7 @@ def test_apply_args(): [((), {}, 0), ((), {"a": 1}, 1), ((2, 3), {}, 32), ((1,), {"c": 2}, 201)], ) def test_agg_args(args, kwargs, increment): + # GH 43357 def f(x, a=0, b=0, c=0): return x + a + 10 * b + 100 * c