pola-rs/polars · error · TypeError
cannot turn into selector
Error message
cannot turn {qualified_type_name(input)!r} into selector What it means
parse_into_expression converts Python inputs into polars expressions. When require_selector is set, the input must be a string, Selector, or Expr; anything else (int, dict, arbitrary object) cannot become a selector, so a TypeError is raised with the qualified type name.
Solutions
- Pass a column name string (converted to F.col) or a pl.selectors.* selector.
- Wrap a literal explicitly with pl.lit(value) if you intended a literal, or pl.col(name) for a column.
- Use pl.Expr.meta.as_selector()-compatible expressions only where selectors are allowed.
- Check the function signature to see whether it expects expressions, selectors, or names.
Example fix
// before
df.select(42)
// after
df.select(pl.col("column_name")) # or df.select(pl.selectors.by_index(42)) Defensive patterns
Strategy: type-guard
Validate before calling
def ensure_selector_input(x):
import polars as pl
if isinstance(x, (str, pl.Selector, pl.Expr)):
return x
raise TypeError(f"cannot use {type(x).__name__} as selector; use str/Selector/Expr") Type guard
def is_selector_input(x) -> bool:
import polars as pl
return isinstance(x, (str, pl.Selector, pl.Expr)) Try / catch
try:
result = df.select(inputs)
except TypeError as e:
if "into selector" in str(e):
result = df.select([pl.lit(v).alias(k) for k, v in inputs.items()])
else:
raise Prevention
- Pass strings or pl.selectors.* objects where selectors are expected.
- Wrap literals with pl.lit and columns with pl.col explicitly.
- Read the signature: require_selector APIs reject raw values.
- Add input validation at API boundaries that forward user data to select-like calls.
When it happens
Trigger: Calling APIs like pl.selectors-consuming methods or functions that route through parse_into_expression(require_selector=True) with a non-selector value, e.g. df.select(42), pl.slice with a non-string, or passing a list of wrong types where selectors are expected.
Common situations: Typos like passing column positions where names are expected, passing dicts/sets instead of column name strings, or mixing literals into selector-only contexts.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- cannot parse input into Polars selector
- cannot turn into selector
- expected a selector; found
- expected one or more `str`, `DataType` or selector; found
- invalid dtype
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/7104bf1a6a1db1f6.
Report an issue: GitHub.
Appendix: source
Thrown at py-polars/src/polars/_utils/parse/expr.py:64
If the input is expected to resolve to a literal with a known dtype, pass
this to the `lit` constructor.
require_selector
Require that the input is a valid selector (eg: column name or selector).
Returns
-------
PyExpr
"""
if isinstance(input, pl.Expr):
expr = input
if structify:
expr = _structify_expression(expr)
elif isinstance(input, str) and not str_as_lit:
expr = F.col(input)
else:
if require_selector:
msg = f"cannot turn {qualified_type_name(input)!r} into selector"
raise TypeError(msg)
elif isinstance(input, list) and list_as_series:
expr = F.lit(pl.Series(input), dtype=dtype)
else:
expr = F.lit(input, dtype=dtype)
return expr._pyexpr
def _structify_expression(expr: Expr) -> Expr:
unaliased_expr = expr.meta.undo_aliases()
if unaliased_expr.meta.has_multiple_outputs():
try:
expr_name = expr.meta.output_name()
except ComputeError:
expr = F.struct(expr)
else:
expr = F.struct(unaliased_expr).alias(expr_name)
return exprView on GitHub (pinned to fe841f959e)