Skip to content

BUG: inconsistent behavior in the DataFrame.agg() when use custom aggregate function #60592

@arlequinfei

Description

@arlequinfei

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    ApplyApply, Aggregate, Transform, MapBug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions