Skip to content
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

BUG: DataFrameGroupBy.agg has different behavior when input function receives keyword argument #60379

Closed
2 of 3 tasks
mldcastro opened this issue Nov 20, 2024 · 1 comment
Closed
2 of 3 tasks
Labels
Apply Apply, Aggregate, Transform, Map Bug duplicated duplicated, drop_duplicates Groupby

Comments

@mldcastro
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({"a": [1, 2], "b": [4, 5]}, index=pd.Index(list("AB"), name="group"))


def _agg_func(*args, **kwargs):
    for arg in args:
        print("ARG:\n")
        print(arg)
        print()
    return None


# _agg_func will receive a pandas Series of column "a", then after it, column "b"
df.groupby(level="group").agg(_agg_func)

# _agg_func will receive a DataFrame with columns "a" and "b" for each group
df.groupby(level="group").agg(_agg_func, some_kwarg="abc")

Issue Description

When performing an aggregation over groups, my colleagues and I observed that DataFrameGroupBy.agg has inconsistent behavior when the function passed to the agg method has or not keyword arguments.

If agg receives the function _agg_func without keyword arguments, then the input to _agg_func will be a pandas.Series. If keyword arguments are passed, then the input to _agg_func is a pandas.DataFrame.

Expected Behavior

As per the docs, the parameter func should be able to receive a pandas.DataFrame as input, hence, I would expect that the input for _agg_func should always be a pandas.DataFrame, independently if the function receives or not a keyword argument.

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.11.10
python-bits : 64
OS : Linux
OS-release : 5.15.133.1-microsoft-standard-WSL2
Version : #1 SMP Thu Oct 5 21:02:42 UTC 2023
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.2.3
numpy : 2.0.1
pytz : 2024.2
dateutil : 2.9.0.post0
pip : 23.3.1
Cython : None
sphinx : None
IPython : 8.29.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : 3.1.4
lxml.etree : None
matplotlib : 3.9.0
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : 2.9.9
pymysql : None
pyarrow : 18.0.0
pyreadstat : None
pytest : 8.3.2
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.14.1
sqlalchemy : 2.0.32
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2024.2
qtpy : None
pyqt5 : None

@mldcastro mldcastro added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 20, 2024
@rhshadrach
Copy link
Member

Thanks for the report, closing as a duplicate of #39169.

@rhshadrach rhshadrach added Groupby Apply Apply, Aggregate, Transform, Map duplicated duplicated, drop_duplicates and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 21, 2024
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 duplicated duplicated, drop_duplicates Groupby
Projects
None yet
Development

No branches or pull requests

2 participants