pandas-dev/pandas · error · TypeError

operation ' ' not supported for dtype

Error message

operation '{name}' not supported for dtype '{self.dtype}'

What it means

StringArray._accumulate raises TypeError specifically for cumprod, since cumulative product is undefined for text. cumsum, cummin, and cummax are supported (string concatenation and lexicographic accumulation), but multiplying strings is not.

Solutions

  1. Remove cumprod from operations applied to string columns.
  2. Convert to numeric first if the strings represent numbers: pd.to_numeric(s, errors='coerce').cumprod().
  3. Use cumsum for string concatenation accumulation if that was the intent.

Example fix

// before
s = pd.Series(['1','2','3'], dtype='string')
s.cumprod()  # TypeError
// after
pd.to_numeric(s, errors='coerce').cumprod()
Defensive patterns

Strategy: type-guard

Validate before calling

def safe_cumprod(s):
    if pd.api.types.is_string_dtype(s):
        s = pd.to_numeric(s, errors='coerce')
    return s.cumprod()

Type guard

def cumprod_safe_dtype(series) -> bool:
    return pd.api.types.is_numeric_dtype(series)

Try / catch

try:
    s.cumprod()
except TypeError as e:
    if 'not supported for dtype' in str(e):
        pd.to_numeric(s, errors='coerce').cumprod()
    else:
        raise

Prevention

When it happens

Trigger: Calling df['col'].cumprod() on a string-dtyped column; invoking Series.cumprod() on dtype 'string' or 'string[pyarrow]'.

Common situations: Applying a generic accumulation suite (cumsum, cumprod, cummin, cummax) across all columns without dtype checks; refactoring a numeric column to string and forgetting cumprod breaks.

Related errors


AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11). Data as JSON: /api/errors/cc1d8790f122443a. Report an issue: GitHub.

Appendix: source

Thrown at pandas/core/arrays/string_.py:1010

            - cumsum
            - cumprod
        skipna : bool, default True
            If True, skip NA values.
        **kwargs
            Additional keyword arguments passed to the accumulation function.
            Currently, there is no supported kwarg.

        Returns
        -------
        array

        Raises
        ------
        NotImplementedError : subclass does not define accumulations
        """
        if name == "cumprod":
            msg = f"operation '{name}' not supported for dtype '{self.dtype}'"
            raise TypeError(msg)

        # We may need to strip out trailing NA values
        tail: np.ndarray | None = None
        na_mask: np.ndarray | None = None
        ndarray = self._ndarray
        np_func = {
            "cumsum": np.cumsum,
            "cummin": np.minimum.accumulate,
            "cummax": np.maximum.accumulate,
        }[name]

        if self._hasna:
            na_mask = cast("npt.NDArray[np.bool_]", isna(ndarray))
            if np.all(na_mask):
                return type(self)(ndarray, dtype=self.dtype)
            if skipna:
                if name == "cumsum":
                    ndarray = np.where(na_mask, "", ndarray)

View on GitHub (pinned to 3b7651241d)