BUG: pivot_table aggfunc should accept string function-likes · Issue #18713 · pandas-dev/pandas (original) (raw)

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
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
In [14]: pd.pivot_table(df, values='D', index=['A', 'B'],
    ...:                      columns=['C'], aggfunc=['sum', 'mean'])
AttributeError: 'str' object has no attribute '__name__'