Skip to content

BUG: DataFrame.agg([...], numeric_only=True) returns NaN for non-numeric columns instead of excluding them #65218

@gautamvarmadatla

Description

@gautamvarmadatla

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, 3],
      "B": [1.0, 2.0, 3.0],
      "C": ["foo", "bar", "baz"], })

result = df.agg(["sum", "mean"], numeric_only=True)
print(result)

Issue Description

DataFrame.agg([...], numeric_only=True) returns NaN for non-numeric columns instead of excluding them. This affects DataFrame.agg with a list of string reduction names and numeric_only=True, including reductions such as sum, mean, std, var, sem, median, prod, min, and max, on DataFrames with non-numeric columns such as object, datetime64[ns], timedelta64[ns], StringDtype, CategoricalDtype, and DatetimeTZDtype.

Expected Behavior

Non-numeric columns should be excluded from the result when DataFrame.agg([...], numeric_only=True) is used, rather than being included as all NaN.

Installed Versions

Details

INSTALLED VERSIONS

commit : 2a7587f
python : 3.12.12
python-bits : 64
OS : Windows
OS-release : 11
Version : 10.0.26200
machine : AMD64
processor : Intel64 Family 6 Model 183 Stepping 1, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 3.1.0.dev0+526.g2a7587fed0.dirty
numpy : 2.4.2
dateutil : 2.9.0.post0
pip : 26.0.1
Cython : 3.2.4
sphinx : None
IPython : 9.10.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : 1.6.0
fastparquet : None
fsspec : None
html5lib : None
hypothesis : 6.151.6
gcsfs : None
jinja2 : None
lxml.etree : None
matplotlib : 3.10.8
numba : None
numexpr : 2.14.1
odfpy : None
openpyxl : None
psycopg2 : None
pymysql : None
pyarrow : None
pyiceberg : None
pyreadstat : None
pytest : 9.0.2
python-calamine : None
pytz : 2025.2
pyxlsb : None
s3fs : None
scipy : 1.17.0
sqlalchemy : 2.0.46
tables : None
tabulate : 0.9.0
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
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