pola-rs/polars · error · TypeError

cannot parse input {input_type} into Polars selector{input_d

Error message

cannot parse input {input_type} into Polars selector{input_detail}

What it means

Selector._by_dtype (the engine behind cs.by_dtype) recognizes polars dtypes and a fixed allowlist of Python types (int, float, bool, str, bytes, object, NoneType, datetime/date/time/timedelta, decimal.Decimal, list, tuple). Inside an iterable input, any OTHER type object (e.g. numpy scalar types, set, a pandas dtype class) falls through to a TypeError. Note the message formats the builtin `input` name rather than the failing element, so the printed 'type' can look confusing.

Source

Thrown at py-polars/src/polars/selectors.py:420

                elif dt is pydatetime.datetime:
                    selectors += [datetime()]
                elif dt is pydatetime.timedelta:
                    selectors += [duration()]
                elif dt is pydatetime.date:
                    selectors += [date()]
                elif dt is PyDecimal:
                    selectors += [decimal()]
                elif dt is builtins.list or dt is tuple:
                    selectors += [list()]
                else:
                    input_type = (
                        input
                        if type(input) is type
                        else f"of type {type(input).__name__!r}"
                    )
                    input_detail = "" if type(input) is type else f" (given: {input!r})"
                    msg = f"cannot parse input {input_type} into Polars selector{input_detail}"
                    raise TypeError(msg) from None
            else:
                input_type = (
                    input
                    if type(input) is type
                    else f"of type {type(input).__name__!r}"
                )
                input_detail = "" if type(input) is type else f" (given: {input!r})"
                msg = f"cannot parse input {input_type} into Polars selector{input_detail}"
                raise TypeError(msg) from None

        dtype_selector = cls._from_pyselector(PySelector.by_dtype(concrete_dtypes))

        if len(selectors) == 0:
            return dtype_selector

        selector = selectors[0]
        for s in selectors[1:]:
            selector = selector | s

View on GitHub (pinned to df599052da)

Solutions

  1. Convert numpy/pandas types to polars dtypes: cs.by_dtype(pl.Float32) or Python builtins (cs.by_dtype(float))
  2. Map numpy types before calling: {np.int64: int, np.float64: float, np.float32: pl.Float32}
  3. Use the ready-made selectors: cs.numeric(), cs.float(), cs.integer()

Example fix

# before
cs.by_dtype([np.float32])

# after
cs.by_dtype(pl.Float32)
Defensive patterns

Strategy: validation

Validate before calling

ALLOWED = {int, float, bool, str, bytes, object, type(None)}
def parseable(dt) -> bool:
    return is_polars_dtype(dt) or dt in ALLOWED
bad = [dt for dt in dtypes if not parseable(dt)]
if bad:
    raise TypeError(f'convert to polars dtypes first: {bad}')

Type guard

from polars.datatypes import is_polars_dtype
import datetime as _dt
import decimal

ALLOWED_PY = {int, float, bool, str, bytes, object, type(None),
              _dt.datetime, _dt.date, _dt.time, _dt.timedelta, decimal.Decimal, list, tuple}

def is_by_dtype_input(x) -> bool:
    return is_polars_dtype(x) or x in ALLOWED_PY

Prevention

When it happens

Trigger: cs.by_dtype([np.float32]); cs.by_dtype([set]); cs.by_dtype((pd.CategoricalDtype,)); any collection containing an unrecognized class.

Common situations: Numpy/pandas dtype objects leaking into dtype lists from cross-library code; teams assuming numpy types are accepted because polars converts them elsewhere (pl.from_numpy).

Related errors


AI-assisted analysis of pola-rs/polars@df599052da (2026-08-16). Data as JSON: /api/errors/4ce323f1361e830f. Report an issue: GitHub.