pola-rs/polars · error · TypeError

unsupported operand type(s) for op: ('Selector' +…

Error message

unsupported operand type(s) for op: ('Selector' + 'Selector')

What it means

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.

Solutions

  1. Use the union operator: cs.numeric() | cs.string().
  2. If you intended element-wise addition of columns, convert to expressions first: cs.numeric().as_expr() + other_expr.
  3. Review the selector for intended set semantics: union |, intersection &, difference -, symmetric difference ^.

Example fix

// before
sel = cs.numeric() + cs.string()
// after
sel = cs.numeric() | cs.string()
Defensive patterns

Strategy: type-guard

Validate before calling

from polars.selectors import is_selector
if is_selector(a) and is_selector(b):
    combined = a | b  # never a + b
else:
    combined = a.as_expr() + b

Type guard

def safe_combine(a, b):
    from polars.selectors import is_selector
    if is_selector(a) and is_selector(b):
        return a | b
    if is_selector(a):
        return a.as_expr() + b
    return a + b.as_expr()

Try / catch

try:
    result = a + b
except TypeError as e:
    if "'Selector' + 'Selector'" in str(e):
        result = a | b
    else:
        raise

Prevention

When it happens

Trigger: Writing cs.numeric() + cs.string() — using + to union two selectors instead of |.

Common situations: 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.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/b51975f285c4e105. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/selectors.py:466

    def _by_name(
        cls, names: builtins.list[str], *, strict: bool, expand_patterns: bool
    ) -> Selector:
        return cls._from_pyselector(PySelector.by_name(names, strict, expand_patterns))

    def __invert__(cls) -> Selector:
        """Invert the selector."""
        return all() - cls

    def __add__(self, other: Any) -> Expr:
        if is_selector(other):
            return self.as_expr().__add__(other.as_expr())
        else:
            return self.as_expr().__add__(other)

    def __radd__(self, other: Any) -> Expr:
        if is_selector(other):
            msg = "unsupported operand type(s) for op: ('Selector' + 'Selector')"
            raise TypeError(msg)
        else:
            return self.as_expr().__radd__(other)

    @overload
    def __and__(self, other: Selector) -> Selector: ...

    @overload
    def __and__(self, other: Any) -> Expr: ...

    def __and__(self, other: Any) -> Selector | Expr:
        if is_selector(other):
            return Selector._from_pyselector(
                PySelector.intersect(self._pyselector, other._pyselector)
            )
        else:
            return self.as_expr().__and__(other)

    def __rand__(self, other: Any) -> Expr:

View on GitHub (pinned to fe841f959e)