pola-rs/polars · error · TypeError

cannot parse input into Polars selector

Error message

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

What it means

Inside Selector._by_dtype (used by cs.by_dtype), each input must be a Polars DataType class/instance or a plain Python type that maps to one (int, float, str...). The loop over collection inputs rejects any element that is not such a type and raises this TypeError, telling you both the offending input type and its value.

Solutions

  1. Replace string dtype names with Polars dtype objects: 'string' -> pl.String, 'int64' -> pl.Int64.
  2. Map strings programmatically: dtypes = [getattr(pl, d.upper()) for d in str_dtypes].
  3. Remove None or non-dtype entries from the collection before calling cs.by_dtype.
  4. Check each element with polars.datatypes.is_polars_dtype(d) or isinstance(d, type) beforehand.

Example fix

// before
cs.by_dtype(["string", "float"])
// after
cs.by_dtype([pl.String, pl.Float64])
Defensive patterns

Strategy: validation

Validate before calling

from polars.datatypes import is_polars_dtype
bad = [t for t in dtypes if not (is_polars_dtype(t) or isinstance(t, type))]
if bad:
    raise ValueError(f"not polars dtypes: {bad!r}")

Type guard

def is_dtype_like(t) -> bool:
    from polars.datatypes import is_polars_dtype
    return is_polars_dtype(t) or isinstance(t, type)

Try / catch

try:
    sel = cs.by_dtype(*dtypes)
except TypeError as e:
    if "cannot parse input" in str(e):
        dtypes = [getattr(pl, str(t).upper()) for t in dtypes if isinstance(t, str)]
        sel = cs.by_dtype(*dtypes)
    else:
        raise

Prevention

When it happens

Trigger: cs.by_dtype([pl.Int64, 'string']) — a string like 'string' or 'Int64' inside the collection instead of pl.String; or nested non-type objects like None or dtype instances of other libraries (numpy.dtypes).

Common situations: Users passing dtype names as strings (common after coming from pandas 'float64' style), or mixing dtypes from configuration files where they were serialized as strings.

Related errors


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

Appendix: source

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

                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 fe841f959e)