python
data_error
ai_generated
true
pandas.core.base.DataError: No numeric types to aggregate
ID: python/pandas-groupby-agg-nonnumeric
80%Fix Rate
88%Confidence
0Evidence
2024-12-03First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
Groupby mean/sum applied to DataFrame with no numeric columns (all object/string).
generic中文
对全为非数值列(object/字符串)的 DataFrame 执行 groupby mean/sum。
Workarounds
-
95% success
df.select_dtypes(include='number').groupby(key).mean()
-
90% success
Convert target column first: df['val'] = pd.to_numeric(df['val'], errors='coerce')
Dead Ends
Common approaches that don't work:
-
70% fail
Returns empty frame when no numeric columns exist
-
80% fail
Fails on non-numeric strings