{"record":{"id":"b51975f285c4e105","repo":"pola-rs/polars","slug":"unsupported-operand-type-s-for-op-selector","errorCode":null,"errorMessage":"unsupported operand type(s) for op: ('Selector' + 'Selector')","messagePattern":"unsupported operand type\\(s\\) for op: \\('Selector' \\+ 'Selector'\\)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/selectors.py","lineNumber":466,"sourceCode":"    def _by_name(\n        cls, names: builtins.list[str], *, strict: bool, expand_patterns: bool\n    ) -> Selector:\n        return cls._from_pyselector(PySelector.by_name(names, strict, expand_patterns))\n\n    def __invert__(cls) -> Selector:\n        \"\"\"Invert the selector.\"\"\"\n        return all() - cls\n\n    def __add__(self, other: Any) -> Expr:\n        if is_selector(other):\n            return self.as_expr().__add__(other.as_expr())\n        else:\n            return self.as_expr().__add__(other)\n\n    def __radd__(self, other: Any) -> Expr:\n        if is_selector(other):\n            msg = \"unsupported operand type(s) for op: ('Selector' + 'Selector')\"\n            raise TypeError(msg)\n        else:\n            return self.as_expr().__radd__(other)\n\n    @overload\n    def __and__(self, other: Selector) -> Selector: ...\n\n    @overload\n    def __and__(self, other: Any) -> Expr: ...\n\n    def __and__(self, other: Any) -> Selector | Expr:\n        if is_selector(other):\n            return Selector._from_pyselector(\n                PySelector.intersect(self._pyselector, other._pyselector)\n            )\n        else:\n            return self.as_expr().__and__(other)\n\n    def __rand__(self, other: Any) -> Expr:","sourceCodeStart":448,"sourceCodeEnd":484,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/selectors.py#L448-L484","documentation":"Selector.__radd__ disallows adding two selectors with '+'. Selectors combine with set operators (|, &, -, ^); when Python falls back to __radd__ (i.e. `selector_a + selector_b` where Selector.__add__ already deferred) and the other operand is also a Selector, this TypeError is raised to steer users to the correct operator.","triggerScenarios":"Writing cs.numeric() + cs.string() — using + to union two selectors instead of |.","commonSituations":"Muscle memory from arithmetic column expressions (pl.col('a') + pl.col('b')) applied to selectors; code that concatenates selectors where sets would have been unioned.","solutions":["Use the union operator: cs.numeric() | cs.string().","If you intended element-wise addition of columns, convert to expressions first: cs.numeric().as_expr() + other_expr.","Review the selector for intended set semantics: union |, intersection &, difference -, symmetric difference ^."],"exampleFix":"// before\nsel = cs.numeric() + cs.string()\n// after\nsel = cs.numeric() | cs.string()","handlingStrategy":"type-guard","validationCode":"from polars.selectors import is_selector\nif is_selector(a) and is_selector(b):\n    combined = a | b  # never a + b\nelse:\n    combined = a.as_expr() + b","typeGuard":"def safe_combine(a, b):\n    from polars.selectors import is_selector\n    if is_selector(a) and is_selector(b):\n        return a | b\n    if is_selector(a):\n        return a.as_expr() + b\n    return a + b.as_expr()","tryCatchPattern":"try:\n    result = a + b\nexcept TypeError as e:\n    if \"'Selector' + 'Selector'\" in str(e):\n        result = a | b\n    else:\n        raise","preventionTips":["Selectors are set-like: use | & - ^ for combining, never arithmetic operators.","Reserve + for column value arithmetic on Exprs, not selector composition.","Lint for `cs.` operands around + in data-manipulation code."],"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"}