pola-rs/polars · error

cannot select columns using Series of type

Error message

cannot select columns using Series of type {dtype}

What it means

When the column-selection key is a pl.Series, _select_columns accepts String (names), integer (indices), and Boolean (mask) dtypes only. Any other Series dtype (e.g. Float64, Categorical, List) cannot designate columns, so polars raises TypeError naming the dtype.

Solutions

  1. Cast name Series to String: df[names.cast(pl.String)].
  2. Cast positional Series to an integer dtype: df[idx.cast(pl.Int64)].
  3. For boolean masks ensure dtype is Boolean: df[mask.cast(pl.Boolean)].
  4. Convert the Series to a plain list first: df[s.to_list()].
  5. Use df.select(...) with pl.col expressions instead of Series getitem.

Example fix

// before
df[pl.Series([0.0, 2.0])]
// after
df[pl.Series([0.0, 2.0]).cast(pl.Int64)]
Defensive patterns

Strategy: type-guard

Validate before calling

key_s = pl.Series(['a', 'b'])
assert key_s.dtype in (pl.String, pl.Boolean) or key_s.dtype.is_integer(), f'bad selector dtype {key_s.dtype}'
df[key_s]

Type guard

def is_column_selector_series(s: 'pl.Series') -> bool:
    return s.dtype == pl.String or s.dtype == pl.Boolean or s.dtype.is_integer()

Try / catch

try:
    out = df[key_series]
except TypeError as e:
    if 'cannot select columns using Series' in str(e):
        fixed = (key_series.cast(pl.Int64) if key_series.dtype.is_float()
                 else key_series.cast(pl.String))
        out = df[fixed]
    else:
        raise

Prevention

When it happens

Trigger: df[pl.Series([1.0, 2.0])]; df[cat_series] where cat_series holds column names as Categorical; df[date_series]; passing a float Series of positions without casting to Int64.

Common situations: Column names loaded as Categorical from a catalog; float indices from numpy/pandas interop that were never cast; accidental pass-through of a data Series instead of the intended name/index Series.

Related errors


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

Appendix: source

Thrown at py-polars/src/polars/_utils/getitem.py:234

        elif isinstance(first, str):
            return _select_columns_by_name(df, key)  # type: ignore[arg-type]
        else:
            msg = f"cannot select columns using Sequence with elements of type {qualified_type_name(first)!r}"
            raise TypeError(msg)

    elif isinstance(key, pl.Series):
        if key.is_empty():
            return df.__class__()
        dtype = key.dtype
        if dtype == String:
            return _select_columns_by_name(df, key)
        elif dtype.is_integer():
            return _select_columns_by_index(df, key)
        elif dtype == Boolean:
            return _select_columns_by_mask(df, key)
        else:
            msg = f"cannot select columns using Series of type {dtype}"
            raise TypeError(msg)

    elif _check_for_numpy(key) and isinstance(key, np.ndarray):
        if key.ndim == 0:
            key = np.atleast_1d(key)
        elif key.ndim != 1:
            msg = "multi-dimensional NumPy arrays not supported as index"
            raise TypeError(msg)

        if len(key) == 0:
            return df.__class__()

        dtype_kind = key.dtype.kind
        if dtype_kind in ("i", "u"):
            return _select_columns_by_index(df, key)
        elif dtype_kind == "b":
            return _select_columns_by_mask(df, key)
        elif isinstance(key[0], str):
            return _select_columns_by_name(df, key)

View on GitHub (pinned to fe841f959e)