pola-rs/polars · error · TypeError

Series name must be a string

Error message

Series name must be a string

What it means

The Series constructor requires the `name` argument to be a string (or omitted). If you pass two positional arguments and the first is not a str, polars raises this TypeError instead of silently coercing.

Solutions

  1. Pass a string name: pl.Series("values", [1,2,3])
  2. Omit the name entirely: pl.Series([1,2,3])
  3. If you meant to pass data only, make the first positional arg the values or use the values keyword
  4. Convert the intended name with str(...) if it's a scalar like an int

Example fix

// before
pl.Series(0, [1, 2, 3])
// after
pl.Series("values", [1, 2, 3])  # or pl.Series([1, 2, 3])
Defensive patterns

Strategy: type-guard

Validate before calling

if not isinstance(name, str):
    raise TypeError(f"name must be str, got {type(name).__name__}")
pl.Series(name, values)

Type guard

def is_str_or_none(x) -> bool:
    return x is None or isinstance(x, str)

Try / catch

try:
    s = pl.Series(name, values)
except TypeError as e:
    if "name must be a string" in str(e):
        s = pl.Series(str(name), values)

Prevention

When it happens

Trigger: pl.Series(123, [1,2,3]) — passing a non-string first argument while also supplying values; pl.Series(some_series, values) passing a Series/DataFrame as the name; swapped-argument mistakes.

Common situations: Intending the two-argument shortcut pl.Series(name, values) but passing a non-str name (e.g. a column index or an existing Series object); copying code where the first arg was a dtype hint.

Related errors


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

Appendix: source

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

        # If 'Unknown' treat as None to trigger type inference
        if dtype == Unknown:
            dtype = None
        elif dtype is not None and not is_polars_dtype(dtype):
            dtype = parse_into_dtype(dtype)

        # Handle case where values are passed as the first argument
        original_name: str | None = None
        if name is None:
            name = ""
        elif isinstance(name, str):
            original_name = name
        else:
            if values is None:
                values = name
                name = ""
            else:
                msg = "Series name must be a string"
                raise TypeError(msg)

        if isinstance(values, Sequence):
            self._s = sequence_to_pyseries(
                name,
                values,
                dtype=dtype,
                strict=strict,
                nan_to_null=nan_to_null,
            )

        elif values is None:
            self._s = sequence_to_pyseries(name, [], dtype=dtype)

        elif _check_for_numpy(values) and isinstance(values, np.ndarray):
            self._s = numpy_to_pyseries(
                name, values, strict=strict, nan_to_null=nan_to_null
            )
            if values.dtype.type in [np.datetime64, np.timedelta64]:

View on GitHub (pinned to fe841f959e)