{"record":{"id":"eaa91dffd7062e31","repo":"pola-rs/polars","slug":"unsupported-operand-type-s-for-op-expr-sel","errorCode":null,"errorMessage":"unsupported operand type(s) for op: ('Expr' - 'Selector')","messagePattern":"unsupported operand type\\(s\\) for op: \\('Expr' - 'Selector'\\)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/selectors.py","lineNumber":520,"sourceCode":"        return self.as_expr().__ror__(other)\n\n    @overload\n    def __sub__(self, other: Selector) -> Selector: ...\n\n    @overload\n    def __sub__(self, other: Any) -> Expr: ...\n\n    def __sub__(self, other: Any) -> Selector | Expr:\n        if is_selector(other):\n            return Selector._from_pyselector(\n                PySelector.difference(self._pyselector, other._pyselector)\n            )\n        else:\n            return self.as_expr().__sub__(other)\n\n    def __rsub__(self, other: Any) -> NoReturn:\n        msg = \"unsupported operand type(s) for op: ('Expr' - 'Selector')\"\n        raise TypeError(msg)\n\n    @overload\n    def __xor__(self, other: Selector) -> Selector: ...\n\n    @overload\n    def __xor__(self, other: Any) -> Expr: ...\n\n    def __xor__(self, other: Any) -> Selector | Expr:\n        if is_selector(other):\n            return Selector._from_pyselector(\n                PySelector.exclusive_or(self._pyselector, other._pyselector)\n            )\n        else:\n            return self.as_expr().__xor__(other)\n\n    def __rxor__(self, other: Any) -> Expr:\n        return self.as_expr().__rxor__(other)\n","sourceCodeStart":502,"sourceCodeEnd":538,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/selectors.py#L502-L538","documentation":"Selector.__rsub__ unconditionally raises TypeError: an Expr can never be the left operand of '-' with a Selector on the right (Selector subtraction is only defined selector-minus-selector or via as_expr()). This is a hard guard against meaningless `expr - selector` combinations.","triggerScenarios":"Writing pl.col('a') - cs.numeric() or any Expr - Selector expression where Python defers to Selector.__rsub__; typically a typo where the operands were swapped.","commonSituations":"Intending to exclude columns (mixing up '-' with .exclude()), or accidentally writing expr - selector instead of selector - expr.","solutions":["Swap operands if difference was intended: cs.numeric() - cs.by_name('a').","To exclude a column from a selector use .exclude: cs.numeric().exclude('a').","If you wanted arithmetic on columns, expand the selector to expressions: df.select(cs.numeric() - pl.lit(1)) or use .as_expr() per column."],"exampleFix":"// before\nexpr = pl.col(\"a\") - cs.numeric()\n// after\nsel = cs.numeric().exclude(\"a\")","handlingStrategy":"type-guard","validationCode":"from polars.selectors import is_selector\nif is_selector(right):\n    if is_selector(left):\n        result = left - right\n    else:\n        raise ValueError(\"Expr - Selector is not supported; use selector - selector or .exclude()\")","typeGuard":"def selector_difference(left, right):\n    from polars.selectors import is_selector\n    if is_selector(left) and is_selector(right):\n        return left - right\n    if is_selector(right):\n        return left.meta.as_expr().exclude(right) if False else None\n    return left - right","tryCatchPattern":"try:\n    result = expr - selector\nexcept TypeError as e:\n    if \"'Expr' - 'Selector'\" in str(e):\n        result = selector.as_expr() - expr\n    else:\n        raise","preventionTips":["Subtraction is only meaningful selector-minus-selector; keep Exprs out of it.","To drop columns use .exclude() or cs.all() - cs.by_name(...).","Double-check operand order when mixing Exprs and selectors."],"tags":["polars","selectors","operators","type-error"],"backgroundTag":"unsupported-operation","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"}