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: inconsistent behavior in the DataFrame.agg() when use custom aggregate function #60592

Closed
2 of 3 tasks
arlequinfei opened this issue Dec 20, 2024 · 1 comment
Closed
2 of 3 tasks
Labels
Apply Apply, Aggregate, Transform, Map Bug

Comments

@arlequinfei
Copy link

arlequinfei commented Dec 20, 2024

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

data = {
    "Product": ["apple", "pear", "banana"],
    "Sales": [100, 150, 200],
    "Price": [1, 1.5, 2],
}
df = pd.DataFrame(data)

def func1(x):
    # x is Series
    y = x.sum()
    return y

def func2(x):
    # x is a single value
    y = 0
    return y


agg_funcs = {
    "Sales": func1,
    "Price": func2
}

df_column_spec_agg = df.agg(agg_funcs)
print(df_column_spec_agg)

Issue Description

in func1() x is Series, but in func2() x is a single value.

and with an error:

ValueError: cannot perform both aggregation and transformation operations simultaneously

Expected Behavior

type(x) should be same in func1() and func2()

Installed Versions

openSUSE Leap 15.6

Python 3.12.8 (main, Dec 11 2024, 08:52:29) [GCC 13.3.0] on linux

INSTALLED VERSIONS

commit : 0691c5c
python : 3.12.8
python-bits : 64
OS : Linux
OS-release : 6.4.0-150600.23.30-default
Version : #1 SMP PREEMPT_DYNAMIC Sat Dec 7 08:37:53 UTC 2024 (8c25a0a)
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.2.3
numpy : 2.2.0
pytz : 2024.2
dateutil : 2.9.0.post0
pip : 24.3.1
Cython : None
sphinx : None
IPython : 8.30.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.3
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.14.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2024.2
qtpy : None
pyqt5 : None

@arlequinfei arlequinfei added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 20, 2024
@rhshadrach rhshadrach added Apply Apply, Aggregate, Transform, Map and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 20, 2024
@rhshadrach
Copy link
Member

Thanks for the report! Confirmed on 2.2.x, but this is fixed on main. The OP gives the output:

Sales    450
Price      0
dtype: int64

Closing.

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
Projects
None yet
Development

No branches or pull requests

2 participants