pola-rs/polars · error · TypeError

Series constructor called with unsupported type

Error message

Series constructor called with unsupported type {type(values).__name__!r} for the `values` parameter

What it means

The Series constructor accepts only Sequences, mappings (for structs), Series, numpy arrays, pandas/pyarrow objects, etc. Any other type for `values` falls through all supported branches and raises this TypeError naming the offending type.

Solutions

  1. Convert to a list: pl.Series(list(values))
  2. For numpy: pl.Series(values) works; for torch: pl.Series(values.numpy())
  3. For a set: pl.Series(sorted(values))
  4. For scalars use pl.Series("name", [value]) or pl.repeat
  5. Upgrade polars if you're passing a type newly supported (e.g. newer Arrow objects)

Example fix

// before
pl.Series({7, 8, 9})
// after
pl.Series(sorted({7, 8, 9}))  # or list(...)
Defensive patterns

Strategy: validation

Validate before calling

SUPPORTED = (list, tuple, range, dict, pl.Series)
if not isinstance(values, SUPPORTED):
    values = list(values)  # converts sets, generators, etc.
pl.Series("name", values)

Type guard

def is_series_values(x) -> bool:
    return isinstance(x, (Sequence, dict, pl.Series)) or hasattr(x, '__array__') or hasattr(x, 'to_numpy')

Try / catch

try:
    s = pl.Series(values)
except TypeError as e:
    if "unsupported type" in str(e):
        s = pl.Series(list(values)) if hasattr(values, '__iter__') else pl.Series([values])

Prevention

When it happens

Trigger: pl.Series({'a': 1}) (plain dict without struct expectations), pl.Series(generator_object) unwrapped? — actually generators unsupported, pl.Series(42), pl.Series(some_set), pl.Series(custom_object), or passing a non-iterable scalar.

Common situations: Passing a set or generator instead of a list; passing a scalar where a list was intended; passing a torch/tensorflow tensor which polars doesn't recognize (convert via .numpy() or tolist()); dict intended as struct but type not recognized in this polars version.

Related errors


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

Appendix: source

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

            )

        elif isinstance(values, pl.DataFrame):
            self._s = dataframe_to_pyseries(
                original_name, values, dtype=dtype, strict=strict
            )

        elif hasattr(values, "__arrow_c_array__"):
            self._s = PySeries.from_arrow_c_array(values)

        elif hasattr(values, "__arrow_c_stream__"):
            self._s = PySeries.from_arrow_c_stream(values)

        else:
            msg = (
                f"Series constructor called with unsupported type {type(values).__name__!r}"
                " for the `values` parameter"
            )
            raise TypeError(msg)

    @property
    def bin(self) -> BinaryNameSpace:
        """Create an object namespace of all binary related methods."""
        return BinaryNameSpace(self)

    @property
    def cat(self) -> CatNameSpace:
        """Create an object namespace of all categorical related methods."""
        return CatNameSpace(self)

    @property
    def dt(self) -> DateTimeNameSpace:
        """Create an object namespace of all datetime related methods."""
        return DateTimeNameSpace(self)

    @property
    def list(self) -> ListNameSpace:

View on GitHub (pinned to fe841f959e)