pola-rs/polars · error · TypeError

unsupported type for

Error message

unsupported type {qualified_type_name(arg)!r} for {arg!r}

What it means

Series.__array_ufunc__ (numpy ufunc dispatch on a Series) only supports inputs it can convert to numpy views: Series, expressions, and numpy-compatible arrays. The given argument's type was not recognized, so polars raises TypeError naming the type and the argument.

Solutions

  1. Convert the argument to a supported type before the ufunc: pl.Series(list) or np.asarray(x)
  2. Check the operand types; replace unsupported objects (e.g. pandas Series -> .to_numpy())
  3. Use polars-native expressions (pl.col(...).exp()) instead of numpy ufuncs when mixing types

Example fix

// before
np.exp(my_list + srs)
// after
np.exp(pl.Series(my_list) + srs)
Defensive patterns

Strategy: type-guard

Validate before calling

supported = (pl.Series, pl.Expr, np.ndarray)
assert all(isinstance(a, supported) or np.isscalar(a) for a in args)

Type guard

def is_ufunc_compatible(a) -> bool:
    return isinstance(a, (pl.Series, pl.Expr, np.ndarray)) or np.isscalar(a)

Try / catch

try:
    out = np.exp(operand)
except TypeError as e:
    if 'unsupported type' in str(e):
        operand = pl.Series(operand)
        out = np.exp(operand)
    else:
        raise

Prevention

When it happens

Trigger: Calling a numpy ufunc (e.g. np.add, np.exp) or Series-shaped constructor path (dict_to_pydf, _sequence_of_sequence_to_pydf, etc.) with an argument of an unsupported type, e.g. a plain Python list, dict, datetime, or a pandas object passed as an operand to a ufunc alongside a pl.Series.

Common situations: Mixing a pl.Series with a pandas Series or list in np.ufunc(...); passing a Python scalar wrapper (np.datetime64 handled, but e.g. Decimal or custom objects not); refactored code that swapped a numpy array for a list.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/505f41c9888eca36. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/series/series.py:1531

        s = self._s

        if method == "__call__":
            if ufunc.nout != 1:
                msg = "only ufuncs that return one 1D array are supported"
                raise NotImplementedError(msg)

            args: list[int | float | np.ndarray[Any, Any]] = []
            for arg in inputs:
                if isinstance(arg, (int, float, np.ndarray)):
                    args.append(arg)
                elif isinstance(arg, Series):
                    phys_arg = arg.to_physical()
                    if phys_arg._s.n_chunks() > 1:
                        phys_arg._s.rechunk(in_place=True)
                    args.append(phys_arg._s.to_numpy_view())  # type: ignore[arg-type]
                else:
                    msg = f"unsupported type {qualified_type_name(arg)!r} for {arg!r}"
                    raise TypeError(msg)

            # Get minimum dtype needed to be able to cast all input arguments to the
            # same dtype.
            dtype_char_minimum: str = np.result_type(*args).char

            # Get all possible output dtypes for ufunc.
            # Input dtypes and output dtypes seem to always match for ufunc.types,
            # so pick all the different output dtypes.
            dtypes_ufunc = [
                input_output_type[-1]
                for input_output_type in ufunc.types
                if supported_numpy_char_code(input_output_type[-1])
            ]

            # Get the first ufunc dtype from all possible ufunc dtypes for which
            # the input arguments can be safely cast to that ufunc dtype.
            for dtype_ufunc in dtypes_ufunc:
                if np.can_cast(dtype_char_minimum, dtype_ufunc):

View on GitHub (pinned to fe841f959e)