Skip to content

REGR: Dataframe.agg no longer accepts positional arguments as of v1.1.0 #36948

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

Closed
pganssle opened this issue Oct 7, 2020 · 0 comments · Fixed by #36950
Closed

REGR: Dataframe.agg no longer accepts positional arguments as of v1.1.0 #36948

pganssle opened this issue Oct 7, 2020 · 0 comments · Fixed by #36950
Labels
Apply Apply, Aggregate, Transform, Map Bug Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@pganssle
Copy link
Contributor

pganssle commented Oct 7, 2020

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.

@pganssle pganssle added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 7, 2020
@simonjayhawkins simonjayhawkins added this to the 1.1.4 milestone Oct 7, 2020
vorhersager pushed a commit to google/madi that referenced this issue Oct 7, 2020
…andas#36948 and PR: pandas-dev/pandas#36950.

Also makes the set_axis call before plotting the gradient series deliberately in place.

PiperOrigin-RevId: 335933611
vorhersager pushed a commit to google/madi that referenced this issue Oct 7, 2020
…andas#36948 and PR: pandas-dev/pandas#36950.

Also makes the set_axis call before plotting the gradient series deliberately in place.

PiperOrigin-RevId: 335933611
pganssle added a commit to pganssle/pandas that referenced this issue Oct 8, 2020
Although `DataFrame.agg` is documented as accepting `func, axis, *args, **kwargs`
with `*args` and `**kwargs` passed to `func`, passing positional
arguments raises a TypeError.

The reason for this is that the internal call to `self._aggregate` uses
a keyword argument (axis) before passing *args and `**kwargs`, and as such the
first positional argument is always interpreted as a second
specification of `axis`, which raises TypeError.

Prior to commit 433c900, TypeErrors
were being suppressed, falling back to `self.apply`, which in v1.1.0
turned into an error.

This fixes issue pandas-devGH-36948.
@jreback jreback added Apply Apply, Aggregate, Transform, Map Regression Functionality that used to work in a prior pandas version and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 8, 2020
pganssle added a commit to pganssle/pandas that referenced this issue Oct 8, 2020
Although `DataFrame.agg` is documented as accepting `func, axis, *args, **kwargs`
with `*args` and `**kwargs` passed to `func`, passing positional
arguments raises a TypeError.

The reason for this is that the internal call to `self._aggregate` uses
a keyword argument (axis) before passing *args and `**kwargs`, and as such the
first positional argument is always interpreted as a second
specification of `axis`, which raises TypeError.

Prior to commit 433c900, TypeErrors
were being suppressed, falling back to `self.apply`, which in v1.1.0
turned into an error.

This fixes issue pandas-devGH-36948.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Apply Apply, Aggregate, Transform, Map Bug Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants