Skip to content

REGR: Allow positional arguments in DataFrame.agg #36950

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ including other versions of pandas.
Fixed regressions
~~~~~~~~~~~~~~~~~
- Fixed regression where attempting to mutate a :class:`DateOffset` object would no longer raise an ``AttributeError`` (:issue:`36940`)
- Fixed regression where :meth:`DataFrame.agg` would fail with :exc:`TypeError` when passed positional arguments to be passed on to the aggregation function (:issue:`36948`).
- Fixed regression in :class:`RollingGroupby` with ``sort=False`` not being respected (:issue:`36889`)

.. ---------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -7406,7 +7406,7 @@ def aggregate(self, func=None, axis=0, *args, **kwargs):

result = None
try:
result, how = self._aggregate(func, axis=axis, *args, **kwargs)
result, how = self._aggregate(func, axis, *args, **kwargs)
except TypeError as err:
exc = TypeError(
"DataFrame constructor called with "
Expand Down
28 changes: 28 additions & 0 deletions pandas/tests/frame/apply/test_frame_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -1463,6 +1463,34 @@ def test_agg_cython_table_raises(self, df, func, expected, axis):
with pytest.raises(expected, match=msg):
df.agg(func, axis=axis)

@pytest.mark.parametrize("axis", [0, 1])
@pytest.mark.parametrize(
"args, kwargs",
[
((1, 2, 3), {}),
((8, 7, 15), {}),
((1, 2), {}),
((1,), {"b": 2}),
((), {"a": 1, "b": 2}),
((), {"a": 2, "b": 1}),
((), {"a": 1, "b": 2, "c": 3}),
],
)
def test_agg_args_kwargs(self, axis, args, kwargs):
def f(x, a, b, c=3):
return x.sum() + (a + b) / c

df = pd.DataFrame([[1, 2], [3, 4]])

if axis == 0:
expected = pd.Series([5.0, 7.0])
else:
expected = pd.Series([4.0, 8.0])

result = df.agg(f, axis, *args, **kwargs)

tm.assert_series_equal(result, expected)

@pytest.mark.parametrize("num_cols", [2, 3, 5])
def test_frequency_is_original(self, num_cols):
# GH 22150
Expand Down