pandas-dev/pandas · error · TypeError

cannot perform {name} with type {self.dtype}

Error message

cannot perform {name} with type {self.dtype}

What it means

Raised by SparseArray._reduce when the reduction name (e.g. 'median', 'prod', 'sem') has no corresponding method on the SparseArray class. The dispatcher looks up getattr(self, name) and, if missing, refuses with the dtype in the message so the caller knows which operation is unsupported for this ExtensionArray type.

Source

Thrown at pandas/core/arrays/sparse/array.py:1571

            self.__dict__.update(state)

    def nonzero(self) -> tuple[npt.NDArray[np.int32]]:
        if self.fill_value == 0:
            return (self.sp_index.indices,)
        else:
            return (self.sp_index.indices[self.sp_values != 0],)

    # ------------------------------------------------------------------------
    # Reductions
    # ------------------------------------------------------------------------

    def _reduce(
        self, name: str, *, skipna: bool = True, keepdims: bool = False, **kwargs
    ):
        method = getattr(self, name, None)

        if method is None:
            raise TypeError(f"cannot perform {name} with type {self.dtype}")

        if name in ("mean", "sum", "min", "max"):
            # these methods handle skipna themselves; dropping NAs beforehand
            # would hide the NA from their skipna=False short-circuit
            result = method(skipna=skipna, **kwargs)
        else:
            if skipna:
                arr = self
            else:
                arr = self.dropna()
            result = getattr(arr, name)(**kwargs)

        if keepdims:
            return type(self)([result], dtype=self.dtype)
        else:
            return result

    def all(self, axis=None, *args, **kwargs):

View on GitHub (pinned to 71959b8cb9)

Solutions

  1. Reduce the dense version: float(sparse_arr.to_dense().median()).
  2. Drop unsupported reduction names from the .agg list, or guard per-column by dtype.
  3. Implement a custom reducer and call it explicitly rather than via the generic _reduce dispatcher.

Example fix

// before
m = sparse_series.median()  # raises 'cannot perform median ...'

// after
m = sparse_series.to_dense().median()
Defensive patterns

Strategy: fallback

Validate before calling

SUPPORTED_SPARSE_REDUCTIONS = {'sum', 'mean', 'min', 'max', 'all', 'any', 'prod'}

def reduce_sparse_safe(arr, name, **kw):
    if name in SUPPORTED_SPARSE_REDUCTIONS and hasattr(arr, name):
        return getattr(arr, name)(**kw)
    return getattr(arr.to_dense(), name)(**kw)

Type guard

def sparse_supports_reduction(arr, name) -> bool:
    return hasattr(arr, name)

Try / catch

try:
    res = sparse_series.agg(name)
except TypeError as e:
    if 'cannot perform' in str(e):
        res = sparse_series.to_dense().agg(name)
    else:
        raise

Prevention

When it happens

Trigger: Series(sparse).median(), df.agg('sem') on a sparse column, np.nanmedian(sparse_series), or df.prod() on a Sparse[int64] column where the op is not implemented.

Common situations: Calling a reduction that pandas implements densely but not for sparse arrays, or applying a generic .agg([...]) list that includes unsupported names across heterogeneous columns.

Related errors


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