5

This maybe a bug, but what is your take on this pandas functionality:

df = pd.DataFrame(np.arange(20).reshape(10,-1), columns=[*'AB'])

def f(x):
    print(type(x))

df.agg(f)

Output:

<class 'pandas.core.series.Series'>
<class 'pandas.core.series.Series'>
A    None
B    None
dtype: object

However, if I wrap function call in the agg method in brackets passing a single function as a list.

df = pd.DataFrame(np.arange(20).reshape(10,-1), columns=[*'AB'])

def f(x):
    print(type(x))


df.agg([f])

Output:

<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
      A     B
      f     f
0  None  None
1  None  None
2  None  None
3  None  None
4  None  None
5  None  None
6  None  None
7  None  None
8  None  None
9  None  None

All that changed was passing the custom function as a single value list.

Scott Boston
  • 147,308
  • 15
  • 139
  • 187

0 Answers0