Skip to content

Commit 4e51287

Browse files
pganssleKevin D Smith
authored and
Kevin D Smith
committed
REGR: Allow positional arguments in DataFrame.agg (pandas-dev#36950)
1 parent 1a334d8 commit 4e51287

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

doc/source/whatsnew/v1.1.4.rst

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ including other versions of pandas.
1515
Fixed regressions
1616
~~~~~~~~~~~~~~~~~
1717
- Fixed regression where attempting to mutate a :class:`DateOffset` object would no longer raise an ``AttributeError`` (:issue:`36940`)
18+
- 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`).
1819
- Fixed regression in :class:`RollingGroupby` with ``sort=False`` not being respected (:issue:`36889`)
1920

2021
.. ---------------------------------------------------------------------------

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7406,7 +7406,7 @@ def aggregate(self, func=None, axis=0, *args, **kwargs):
74067406

74077407
result = None
74087408
try:
7409-
result, how = self._aggregate(func, axis=axis, *args, **kwargs)
7409+
result, how = self._aggregate(func, axis, *args, **kwargs)
74107410
except TypeError as err:
74117411
exc = TypeError(
74127412
"DataFrame constructor called with "

pandas/tests/frame/apply/test_frame_apply.py

+28
Original file line numberDiff line numberDiff line change
@@ -1463,6 +1463,34 @@ def test_agg_cython_table_raises(self, df, func, expected, axis):
14631463
with pytest.raises(expected, match=msg):
14641464
df.agg(func, axis=axis)
14651465

1466+
@pytest.mark.parametrize("axis", [0, 1])
1467+
@pytest.mark.parametrize(
1468+
"args, kwargs",
1469+
[
1470+
((1, 2, 3), {}),
1471+
((8, 7, 15), {}),
1472+
((1, 2), {}),
1473+
((1,), {"b": 2}),
1474+
((), {"a": 1, "b": 2}),
1475+
((), {"a": 2, "b": 1}),
1476+
((), {"a": 1, "b": 2, "c": 3}),
1477+
],
1478+
)
1479+
def test_agg_args_kwargs(self, axis, args, kwargs):
1480+
def f(x, a, b, c=3):
1481+
return x.sum() + (a + b) / c
1482+
1483+
df = pd.DataFrame([[1, 2], [3, 4]])
1484+
1485+
if axis == 0:
1486+
expected = pd.Series([5.0, 7.0])
1487+
else:
1488+
expected = pd.Series([4.0, 8.0])
1489+
1490+
result = df.agg(f, axis, *args, **kwargs)
1491+
1492+
tm.assert_series_equal(result, expected)
1493+
14661494
@pytest.mark.parametrize("num_cols", [2, 3, 5])
14671495
def test_frequency_is_original(self, num_cols):
14681496
# GH 22150

0 commit comments

Comments
 (0)