pola-rs/polars · error · TypeError
expected a selector; found {selector!r} instead.
Error message
expected a selector; found {selector!r} instead. What it means
polars.selectors.expand_selector requires an actual selector: with strict=True (default) the value must pass is_selector (a polars Selector). With strict=False it may be any column-selection expression (meta.is_column_selection(allow_aliasing=False)). Strings, plain columns like pl.col('a') under strict mode, and computed expressions all fail.
Source
Thrown at py-polars/src/polars/selectors.py:185
Allow for non-strict selection expressions (such as those
including use of an `.exclude()` constraint) to be expanded:
>>> cs.expand_selector(schema, cs.numeric().exclude("id"), strict=False)
('count', 'value')
"""
if isinstance(target, Mapping):
from polars.dataframe import DataFrame
target = DataFrame(schema=target)
if not (
is_selector(selector)
if strict
else selector.meta.is_column_selection(allow_aliasing=False)
):
msg = f"expected a selector; found {selector!r} instead."
raise TypeError(msg)
return tuple(target.select(selector).collect_schema())
# TODO: Don't use this as it collects a schema (can be very expensive for LazyFrame).
# This should move to IR conversion / Rust.
def _expand_selectors(frame: DataFrame | LazyFrame, *items: Any) -> builtins.list[str]:
"""
Internal function that expands any selectors to column names in the given input.
Non-selector values are left as-is.
Examples
--------
>>> from polars.selectors import _expand_selectors
>>> import polars.selectors as cs
>>> df = pl.DataFrame(
... {View on GitHub (pinned to df599052da)
Solutions
- Pass a real selector: expand_selector(df, cs.by_name('a'))
- For plain column expressions allow non-strict mode: expand_selector(df, pl.col('a'), strict=False)
- Replace pl.all() with cs.all() when a Selector type is required
Example fix
# before
expand_selector(df, pl.col('a')) # strict -> TypeError
# after
expand_selector(df, cs.by_name('a'))
# or, for a plain column selection expr:
expand_selector(df, pl.col('a'), strict=False) Defensive patterns
Strategy: type-guard
Validate before calling
from polars.selectors import is_selector
assert is_selector(sel), f'got {sel!r}; wrap with cs.by_name()/cs.all()' Type guard
from polars.selectors import is_selector
def requires_selector(sel) -> bool:
return is_selector(sel) Prevention
- Distinguish Expr from Selector: cs.* factories return Selectors, pl.col()/pl.all() do not
- Use strict=False only when accepting any column-selection expression is intended
When it happens
Trigger: expand_selector(df, 'a'); expand_selector(df, pl.col('a')) with default strict=True; expand_selector(df, pl.col('a') + 1) in any mode; passing pl.all() (an Expr, not a Selector) where cs.all() is required.
Common situations: Internal/shared helper code that accepts 'selectors or column-like things' and forwards unconverted values; pandas-influenced code passing strings; confusing pl.all() with cs.all().
Related errors
- expected one or more `str`, `DataType` or selector; found {i
- cannot parse input {input_type} into Polars selector{input_d
- unsupported operand type(s) for op: ('Selector' + 'Selector'
- unsupported operand type(s) for op: ('Expr' - 'Selector')
- `new` argument is required if `old` argument is not a Mappin
AI-assisted analysis of pola-rs/polars@df599052da (2026-08-16).
Data as JSON: /api/errors/6dbf322aa3533374.
Report an issue: GitHub.