Skip to content

BUG: pivot_table aggfunc should accept string function-likes #18713

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
jreback opened this issue Dec 10, 2017 · 4 comments · Fixed by #18810
Closed

BUG: pivot_table aggfunc should accept string function-likes #18713

jreback opened this issue Dec 10, 2017 · 4 comments · Fixed by #18810
Labels
Compat pandas objects compatability with Numpy or Python functions Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@jreback
Copy link
Contributor

jreback commented Dec 10, 2017

Virtually everywhere we allow strings to substitue for functions in .agg, .apply and such, straightforward to add here. Just need to call https://github.com/pandas-dev/pandas/blob/master/pandas/core/base.py#L627.

example from pd.pivot_table doc-string

In [11]: >>> df = pd.DataFrame({"A": ["foo", "foo", "foo", "foo", "foo",
    ...: ...                          "bar", "bar", "bar", "bar"],
    ...: ...                    "B": ["one", "one", "one", "two", "two",
    ...: ...                          "one", "one", "two", "two"],
    ...: ...                    "C": ["small", "large", "large", "small",
    ...: ...                          "small", "large", "small", "small",
    ...: ...                          "large"],
    ...: ...                    "D": [1, 2, 2, 3, 3, 4, 5, 6, 7]})
    ...: 

In [12]: df
Out[12]: 
     A    B      C  D
0  foo  one  small  1
1  foo  one  large  2
2  foo  one  large  2
3  foo  two  small  3
4  foo  two  small  3
5  bar  one  large  4
6  bar  one  small  5
7  bar  two  small  6
8  bar  two  large  7

works

In [13]: pd.pivot_table(df, values='D', index=['A', 'B'],
    ...:                      columns=['C'], aggfunc=[np.sum, np.mean])
 Out[13]: 
          sum        mean      
C       large small large small
A   B                          
bar one   4.0   5.0   4.0   5.0
    two   7.0   6.0   7.0   6.0
foo one   4.0   1.0   2.0   1.0
    two   NaN   6.0   NaN   3.0

fails

In [14]: pd.pivot_table(df, values='D', index=['A', 'B'],
    ...:                      columns=['C'], aggfunc=['sum', 'mean'])
AttributeError: 'str' object has no attribute '__name__'
@jreback jreback added Compat pandas objects compatability with Numpy or Python functions Difficulty Intermediate Reshaping Concat, Merge/Join, Stack/Unstack, Explode labels Dec 10, 2017
@jreback jreback added this to the Next Major Release milestone Dec 10, 2017
@bobhaffner
Copy link
Contributor

Hi @jreback, I was looking at this today. I believe the line throwing the error is

keys.append(func.__name__)

I would propose something like the following and would be happy to submit a PR. LMK

if isinstance(func, compat.string_types):
    keys.append(func)
else:
    keys.append(func.__name__)

@jreback
Copy link
Contributor Author

jreback commented Dec 16, 2017

sure

@jreback
Copy link
Contributor Author

jreback commented Dec 16, 2017

you can also use

getattr(func, ‘name’, func)

@bobhaffner
Copy link
Contributor

Good deal, I like that better.

@jreback jreback modified the milestones: Next Major Release, 0.22.0 Dec 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Compat pandas objects compatability with Numpy or Python functions Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants