pandas-dev/pandas · error · TypeError

dtype '{self.dtype}' does not support operation '{how}'

Error message

dtype '{self.dtype}' does not support operation '{how}'

What it means

Raised in _groupby_op for a StringDtype column when the requested groupby aggregation is inherently numeric (prod, mean, median, cumsum, cumprod, std, sem, var, skew). String columns cannot participate in these reductions, so pandas rejects them before dispatch.

Source

Thrown at pandas/core/arrays/arrow/array.py:3516

        has_dropped_na: bool,
        min_count: int,
        ngroups: int,
        ids: npt.NDArray[np.intp],
        **kwargs,
    ):
        if isinstance(self.dtype, StringDtype):
            if how in [
                "prod",
                "mean",
                "median",
                "cumsum",
                "cumprod",
                "std",
                "sem",
                "var",
                "skew",
            ]:
                raise TypeError(
                    f"dtype '{self.dtype}' does not support operation '{how}'"
                )
            # Fall through to Arrow-native path below

        pa_type = self._pa_array.type

        # Try PyArrow-native path for decimal and string types where it's faster.
        # For integer/float/boolean, the fallback path via _to_masked() is faster.
        if (
            pa.types.is_decimal(pa_type)
            or pa.types.is_string(pa_type)
            or pa.types.is_large_string(pa_type)
        ):
            native_result = self._groupby_op_pyarrow(
                how=how,
                min_count=min_count,
                ngroups=ngroups,
                ids=ids,

View on GitHub (pinned to 71959b8cb9)

Solutions

  1. Select only numeric columns before aggregating: `df.groupby('g')[numeric_cols].prod()`.
  2. Drop or exclude string columns from the aggregation.
  3. Cast genuinely-numeric string columns to a numeric dtype first.

Example fix

// before
df = pd.DataFrame({"g": ["a", "a", "b"], "x": ["1", "2", "3"]}, dtype="string[pyarrow]")
df.groupby("g").sum()

// after
df["x"] = df["x"].astype("int64[pyarrow]")
df.groupby("g").sum()
Defensive patterns

Strategy: validation

Validate before calling

NUMERIC_GB_OPS = {"sum", "prod", "mean", "median", "std", "var", "sem", "skew", "cumsum", "cumprod", "cummax", "cummin"}

def select_numeric_for_op(df, op):
    if op in NUMERIC_GB_OPS:
        return df.select_dtypes(include="number")
    return df

Type guard

def column_supports_op(series, how) -> bool:
    import pandas as pd
    if isinstance(series.dtype, pd.StringDtype) and how in {"prod", "mean", "median", "cumsum", "cumprod", "std", "sem", "var", "skew"}:
        return False
    return True

Try / catch

try:
    df.groupby("g").prod()
except TypeError as e:
    if "does not support operation" in str(e):
        df.groupby("g")[df.select_dtypes("number").columns].prod()
    else:
        raise

Prevention

When it happens

Trigger: Calling `df.groupby('g').prod()` (or mean/median/cumsum/etc.) on a DataFrame that contains a `string[pyarrow]` column among its value columns.

Common situations: Applying a numeric aggregation across all columns without selecting numeric ones, or after a column was auto-inferred as string.

Related errors


AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07). Data as JSON: /api/errors/fdd768c5c103304c. Report an issue: GitHub.