pandas-dev / pandas

Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
https://pandas.pydata.org
BSD 3-Clause "New" or "Revised" License
43.26k stars 17.79k forks source link

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

Closed AAG-AZTEC closed 7 months ago

AAG-AZTEC commented 7 months ago

Pandas version checks

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 : f538741432edf55c6b9fb5d0d496d2dd1d7c2457 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
Moutlou888 commented 7 months ago

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

rhshadrach commented 6 months ago

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 commented 3 months ago

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)

ChandanMalakar commented 3 months ago

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 commented 2 months ago

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 commented 2 months ago

not working for me.

talkativewarrior commented 1 month ago

@sarwesh123 This works like magic. Thank you so much

Dineshbabu0603 commented 1 month ago

I"m using pandas version 2.2.2 still its not working