{"record":{"id":"6dbf322aa3533374","repo":"pola-rs/polars","slug":"expected-a-selector-found-selector-r-instead","errorCode":null,"errorMessage":"expected a selector; found {selector!r} instead.","messagePattern":"expected a selector; found (.+?) instead\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/selectors.py","lineNumber":186,"sourceCode":"\n    Allow for non-strict selection expressions (such as those\n    including use of an `.exclude()` constraint) to be expanded:\n\n    >>> cs.expand_selector(schema, cs.numeric().exclude(\"id\"), strict=False)\n    ('count', 'value')\n    \"\"\"\n    if isinstance(target, Mapping):\n        from polars.dataframe import DataFrame\n\n        target = DataFrame(schema=target)\n\n    if not (\n        is_selector(selector)\n        if strict\n        else selector.meta.is_column_selection(allow_aliasing=False)\n    ):\n        msg = f\"expected a selector; found {selector!r} instead.\"\n        raise TypeError(msg)\n\n    return tuple(target.select(selector).collect_schema())\n\n\n# TODO: Don't use this as it collects a schema (can be very expensive for LazyFrame).\n#  This should move to IR conversion / Rust.\ndef _expand_selectors(frame: DataFrame | LazyFrame, *items: Any) -> builtins.list[str]:\n    \"\"\"\n    Internal function that expands any selectors to column names in the given input.\n\n    Non-selector values are left as-is.\n\n    Examples\n    --------\n    >>> from polars.selectors import _expand_selectors\n    >>> import polars.selectors as cs\n    >>> df = pl.DataFrame(\n    ...     {","sourceCodeStart":168,"sourceCodeEnd":204,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/selectors.py#L168-L204","documentation":"polars.selectors.expand_selector() requires its `selector` argument to be a genuine ColumnSelector (or, with strict=False, an expression that resolves purely to a column selection). When passed anything else — a plain string, column name list, Expr with arithmetic, etc. — it raises this TypeError instead of attempting an invalid selection.","triggerScenarios":"Calling cs.expand_selector(df, 'colname') or cs.expand_selector(df, pl.col('a') + 1) — i.e. passing a str, Expr, or other non-selector object. With strict=False, passing an Expr that aliases, renames, or computes values (meta.is_column_selection is False).","commonSituations":"Confusing expand_selector with select(); refactoring code where a string column name was previously accepted; dynamically built expressions that accidentally lose selector status; passing a cs.exclude(...) result into expand_selector without strict=False.","solutions":["Wrap the column name in a selector: use cs.by_name('colname') instead of the bare string.","If the argument may be a plain name, convert conditionally: selector = cs.by_name(x) if isinstance(x, str) else x.","For expressions involving exclude or aliasing, pass strict=False only if the expression is still a pure column selection.","Verify with polars.selectors.is_selector(obj) before calling expand_selector."],"exampleFix":"// before\ncols = cs.expand_selector(df, \"colx\")\n// after\ncols = cs.expand_selector(df, cs.by_name(\"colx\"))","handlingStrategy":"type-guard","validationCode":"from polars.selectors import is_selector\nif not is_selector(sel):\n    sel = cs.by_name(sel) if isinstance(sel, str) else None\nif sel is None:\n    raise ValueError(f\"not a selector: {sel!r}\")","typeGuard":"def is_valid_selector(obj) -> bool:\n    from polars.selectors import is_selector\n    return is_selector(obj) or (hasattr(obj, 'meta') and obj.meta.is_column_selection(allow_aliasing=False))","tryCatchPattern":"try:\n    cols = cs.expand_selector(df, sel)\nexcept TypeError as e:\n    if \"expected a selector\" in str(e):\n        cols = cs.expand_selector(df, cs.by_name(sel))\n    else:\n        raise","preventionTips":["Always wrap raw column names with cs.by_name before expand_selector.","Use polars.selectors.is_selector() as a precondition assert in helper functions.","Remember strict=False only relaxes to pure-column-selection Exprs, not strings."],"tags":["polars","selectors","type-error"],"backgroundTag":"type-mismatch","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}