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: agg function failed [how->mean,dtype->object] #57031

Closed
3 tasks
AAG-AZTEC opened this issue Jan 23, 2024 · 9 comments
Closed
3 tasks

BUG: agg function failed [how->mean,dtype->object] #57031

AAG-AZTEC opened this issue Jan 23, 2024 · 9 comments
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@AAG-AZTEC
Copy link

AAG-AZTEC commented Jan 23, 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 numpy as np
import pandas as pd
data = {'planet': ['Mercury', 'Venus', 'Earth', 'Mars',
                   'Jupiter', 'Saturn', 'Uranus', 'Neptune'],
        'radius_km': [2440, 6052, 6371, 3390, 69911, 58232,
                     25362, 24622],
        'moons': [0, 0, 1, 2, 80, 83, 27, 14],
        'type': ['terrestrial', 'terrestrial', 'terrestrial', 'terrestrial',
                 'gas giant', 'gas giant', 'ice giant', 'ice giant'],
        'rings': ['no', 'no', 'no', 'no', 'yes', 'yes', 'yes','yes'],
        'mean_temp_c': [167, 464, 15, -65, -110, -140, -195, -200],
        'magnetic_field': ['yes', 'no', 'yes', 'no', 'yes', 'yes', 'yes', 'yes']
        }
planets = pd.DataFrame(data)
planets
planets.groupby(['type', 'magnetic_field']).mean()

Issue Description

The .groupby is not excluding the strings(objects) in this dataframe. In the older versions (1.3.5 pandas), this works for mean, min, max and even aggregate function.

Expected Behavior

agg function failed [how->mean,dtype->object]

Installed Versions

INSTALLED VERSIONS

commit : f538741
python : 3.12.1.final.0
python-bits : 64
OS : Windows
OS-release : 11
Version : 10.0.22631
machine : AMD64
processor : Intel64 Family 6 Model 140 Stepping 1, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United Kingdom.1252

pandas : 2.2.0
numpy : 1.26.3
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 69.0.3
pip : 23.3.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 5.0.1
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.19.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.2
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.8.2
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
pyarrow : 14.0.2
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.4
qtpy : None
pyqt5 : None

@AAG-AZTEC AAG-AZTEC added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 23, 2024
@Moutlou888
Copy link

I am too keep getting the same error while using this line ----> products.groupby(["Brand"]).mean()

@rhshadrach
Copy link
Member

rhshadrach commented Feb 24, 2024

This behavior was changed in pandas 2.0: https://pandas.pydata.org/docs/whatsnew/v1.5.0.html#numeric-only-default-value

Specify .mean(numeric_only=True) if you'd like to keep the old behavior.

@sarwesh123
Copy link

Use this it will work fine

Assuming 'df' is your DataFrame and 'grouping_columns' are the columns you're grouping by

result = df.groupby(grouping_columns).mean(numeric_only=True)

@askchandan
Copy link

what if you're using .agg() function to perform multiple operations on the grouped columns.

eg : movies.groupby(['Director', 'Star1']).agg(['min', 'max', 'mean'])

It shows the same error (TypeError: agg function failed [how->mean,dtype->object])

Even after I used the numeric_only = True parameter, it returned

TypeError: Cannot use numeric_only=True with SeriesGroupBy.min and non-numeric dtypes.

Please help

@HardWorker-DaSc
Copy link

what if you're using .agg() function to perform multiple operations on the grouped columns.

eg : movies.groupby(['Director', 'Star1']).agg(['min', 'max', 'mean'])

It shows the same error (TypeError: agg function failed [how->mean,dtype->object])

Even after I used the numeric_only = True parameter, it returned

TypeError: Cannot use numeric_only=True with SeriesGroupBy.min and non-numeric dtypes.

Please help

you are trying to use the min() method on a data group that contains non-numeric (atypical) data types
you can use the .select_type() method to select only numeric columns before performing the addition operation.

df.select_types(include='number')

the .select_types(include='number') method selects only numeric columns from the Data Frame df

@deepakyadavfad
Copy link

not working for me.

@talkativewarrior
Copy link

@sarwesh123 This works like magic. Thank you so much

@Dineshbabu0603
Copy link

I"m using pandas version 2.2.2 still its not working

@Chakri3434
Copy link

used this :
if our df is DataFrame ------>
df.groupby(['Dataframe']).mean(numeric_only = True)

this feature is changed and readily available in documentation :)

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

10 participants