Skip to content

BUG: .groupby.agg inconsistent passed object to udf with kwarg presence #47863

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
2 of 3 tasks
camriddell opened this issue Jul 26, 2022 · 1 comment
Closed
2 of 3 tasks
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@camriddell
Copy link

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd

df = pd.DataFrame({
    'group': [*('a' * 5), *('b' * 5)],
    'value1': range(10),
    'value2': range(11, 21),
})

def ufunc(s, irrelevant=None):
    print(type(s))
    print(s)

grouped = df.groupby('group')

print('--- no kwargs passed to ufunc')
grouped.agg(ufunc)

print()

print('--- passing kwarg to ufunc')
grouped.agg(ufunc, irrelevant=None)


Produces:
```python
--- no kwargs passed to ufunc
<class 'pandas.core.series.Series'>
0    0
1    1
2    2
3    3
4    4
Name: value1, dtype: int64
<class 'pandas.core.series.Series'>
5    5
6    6
7    7
8    8
9    9
Name: value1, dtype: int64
<class 'pandas.core.series.Series'>
0    11
1    12
2    13
3    14
4    15
Name: value2, dtype: int64
<class 'pandas.core.series.Series'>
5    16
6    17
7    18
8    19
9    20
Name: value2, dtype: int64

--- passing kwarg to ufunc
<class 'pandas.core.frame.DataFrame'>
  group  value1  value2
0     a       0      11
1     a       1      12
2     a       2      13
3     a       3      14
4     a       4      15
<class 'pandas.core.frame.DataFrame'>
  group  value1  value2
5     b       5      16
6     b       6      17
7     b       7      18
8     b       8      19
9     b       9      20


### Issue Description

I couldn't find an open issue that was the exact same as this. But it seems that `groupby(…).agg` with a user defined function seems to change the object passed into the function depending on the presence of keyword arguments.

In the example above, I am taking the same `Groupby` object and passing a udf into the `.agg` method. As highlighted by the documentation, I would expect each group to be passed as a `DataFrame`. However what is unexpected is that:

1. In the first sample output, we can see each grouped `Series` is into the UDF.
2. In the second sample output where I pass a keyword argument to the UDF, it seems that the entire `DataFrame` is passed into the udf.

### Expected Behavior

I would expect that the presence of keyword arguments would not change the input type of the passed objects to a `Groupby.agg`. Moreover, according to [DataFrame.GroupBy.aggregate](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.core.groupby.DataFrameGroupBy.aggregate.html) udf's should operate on `DataFrame`s and not individual `Series`.

Expected output (created by swapping `.agg` for `.apply`):
```python
--- no kwargs passed to ufunc
<class 'pandas.core.frame.DataFrame'>
  group  value1  value2
0     a       0      11
1     a       1      12
2     a       2      13
3     a       3      14
4     a       4      15
<class 'pandas.core.frame.DataFrame'>
  group  value1  value2
5     b       5      16
6     b       6      17
7     b       7      18
8     b       8      19
9     b       9      20

--- passing kwarg to ufunc
<class 'pandas.core.frame.DataFrame'>
  group  value1  value2
0     a       0      11
1     a       1      12
2     a       2      13
3     a       3      14
4     a       4      15
<class 'pandas.core.frame.DataFrame'>
  group  value1  value2
5     b       5      16
6     b       6      17
7     b       7      18
8     b       8      19
9     b       9      20

Installed Versions

Version : #1 SMP Fri, 22 Jul 2022 23:29:06 +0000
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.4.3
numpy : 1.21.5
pytz : 2021.3
dateutil : 2.8.2
setuptools : 58.1.0
pip : 21.2.4
Cython : None
pytest : 7.0.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.8.0
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.3
IPython : 8.0.1
pandas_datareader: None
bs4 : 4.10.0
bottleneck : None
brotli : None
fastparquet : None
fsspec : 2022.5.0
gcsfs : None
markupsafe : 2.0.1
matplotlib : 3.5.1
numba : None
numexpr : None
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : 8.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.7.3
snappy : None
sqlalchemy : 1.4.32
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None

@camriddell camriddell added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 26, 2022
@camriddell
Copy link
Author

Duplicate of #47701

@camriddell camriddell closed this as not planned Won't fix, can't repro, duplicate, stale Jul 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

1 participant