{"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":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/selectors.py","lineNumber":185,"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":167,"sourceCodeEnd":203,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/selectors.py#L167-L203","documentation":"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.","triggerScenarios":"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.","commonSituations":"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().","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"],"exampleFix":"# before\nexpand_selector(df, pl.col('a'))  # strict -> TypeError\n\n# after\nexpand_selector(df, cs.by_name('a'))\n# or, for a plain column selection expr:\nexpand_selector(df, pl.col('a'), strict=False)","handlingStrategy":"type-guard","validationCode":"from polars.selectors import is_selector\nassert is_selector(sel), f'got {sel!r}; wrap with cs.by_name()/cs.all()'","typeGuard":"from polars.selectors import is_selector\n\ndef requires_selector(sel) -> bool:\n    return is_selector(sel)","tryCatchPattern":null,"preventionTips":["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"],"tags":["polars","selectors","type-error","internal-api"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}