pandas-dev/pandas · error · TypeError
Cannot perform reduction '{name}' with string dtype
Error message
Cannot perform reduction '{name}' with string dtype What it means
StringArray._reduce supports only a fixed set of reductions: any, all, count, min, max, argmin, argmax, sum. Any other reduction name (mean, median, std, var, prod, sem, skew) raises TypeError because those operations are not defined for string data.
Source
Thrown at pandas/core/arrays/string_.py:978
skipna: bool = True,
keepdims: bool = False,
axis: AxisInt | None = 0,
**kwargs,
):
if self.dtype.na_value is np.nan and name in ["any", "all"]:
if name == "any":
return nanops.nanany(self._ndarray, skipna=skipna)
else:
return nanops.nanall(self._ndarray, skipna=skipna)
elif name == "count":
return super().count()
elif name in ["min", "max", "argmin", "argmax", "sum"]:
result = getattr(self, name)(skipna=skipna, axis=axis, **kwargs)
if keepdims:
return self._from_sequence([result], dtype=self.dtype)
return result
raise TypeError(f"Cannot perform reduction '{name}' with string dtype")
def _accumulate(self, name: str, *, skipna: bool = True, **kwargs) -> StringArray:
"""
Return an ExtensionArray performing an accumulation operation.
The underlying data type might change.
Parameters
----------
name : str
Name of the function, supported values are:
- cummin
- cummax
- cumsum
- cumprod
skipna : bool, default True
If True, skip NA values.
**kwargsView on GitHub (pinned to 71959b8cb9)
Solutions
- Select only numeric columns before applying numeric reductions (e.g., df.select_dtypes('number')).
- If the strings encode numbers, convert first: s.astype(float).mean().
- Use the supported reductions (count, min, max, sum, any, all) for string data.
Example fix
// before string_series.mean() // after string_series.astype(float).mean()
Defensive patterns
Strategy: validation
Validate before calling
SUPPORTED = {'any', 'all', 'count', 'min', 'max', 'argmin', 'argmax', 'sum'}
if name not in SUPPORTED:
raise TypeError(f'Reduction {name} not supported for string dtype') Type guard
SUPPORTED = {'any', 'all', 'count', 'min', 'max', 'argmin', 'argmax', 'sum'}
def is_supported_string_reduction(name: str) -> bool:
return name in SUPPORTED Prevention
- Select numeric columns before applying numeric reductions (select_dtypes('number')).
- Convert numeric strings via astype(float) before numeric reduction.
- Whitelist supported reductions for string columns in generic aggregation code.
When it happens
Trigger: Calling string_series.mean(), string_series.median(), string_series.std(), string_series.prod(), string_series.sem(), or any numeric-only reduction on a string-typed Series/array.
Common situations: Generic describe()/agg() pipelines that apply numeric reductions to every column; statistical code run over a DataFrame that includes string columns.
Related errors
- timedelta64 type does not support {how} operations
- 'std' and 'sem' are not valid for PeriodDtype
- Cannot change data-type for string array.
- Invalid value '{value}' for dtype '{self.dtype}'. Value shou
- Invalid value for dtype 'str'. Value should be a string or m
AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07).
Data as JSON: /api/errors/81e47015614b963f.
Report an issue: GitHub.