REGR: Dataframe.agg no longer accepts positional arguments as of v1.1.0 · Issue #36948 · pandas-dev/pandas (original) (raw)
Currently, passing any positional arguments to the *args
parameter of DataFrame.agg
fails with a TypeError
, but the documented behavior is that positional and keyword arguments are passed on to the function that you are aggregating with. A minimal reproducer:
import pandas as pd
def f(x, a): return x.sum() + a
df = pd.DataFrame([1, 2])
print(df.agg(f, 0, 3))
This is a regression introduced in v1.1.0, which was introduced in 433c900 (GH-34377), and it was mainly because the previous code only incidentally worked. Prior to the "offending" commit, all TypeErrors were caught and .agg
would fall back to apply
. This was incidentally (as far as I can tell) catching the TypeError
raised from the fact that the self._aggregate
call passed axis
as a keyword argument, so:
self._aggregate(func, axis=axis, *args, **kwargs)
...
def aggregate(self, func, axis, *args, **kwargs): ...
This means that aggregate
takes func
and the first positional argument from *args
as func
and axis
, then takes axis=1
to be axis
, raising TypeError
because axis
is specified twice.
I believe the solution here is to pass axis
to self._aggregate
by position.