pola-rs/polars · error · TypeError

unsupported operand type(s) for op: ('Expr' - 'Selector')

Error message

unsupported operand type(s) for op: ('Expr' - 'Selector')

What it means

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.

Solutions

  1. Swap operands if difference was intended: cs.numeric() - cs.by_name('a').
  2. To exclude a column from a selector use .exclude: cs.numeric().exclude('a').
  3. If you wanted arithmetic on columns, expand the selector to expressions: df.select(cs.numeric() - pl.lit(1)) or use .as_expr() per column.

Example fix

// before
expr = pl.col("a") - cs.numeric()
// after
sel = cs.numeric().exclude("a")
Defensive patterns

Strategy: type-guard

Validate before calling

from polars.selectors import is_selector
if is_selector(right):
    if is_selector(left):
        result = left - right
    else:
        raise ValueError("Expr - Selector is not supported; use selector - selector or .exclude()")

Type guard

def selector_difference(left, right):
    from polars.selectors import is_selector
    if is_selector(left) and is_selector(right):
        return left - right
    if is_selector(right):
        return left.meta.as_expr().exclude(right) if False else None
    return left - right

Try / catch

try:
    result = expr - selector
except TypeError as e:
    if "'Expr' - 'Selector'" in str(e):
        result = selector.as_expr() - expr
    else:
        raise

Prevention

When it happens

Trigger: 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.

Common situations: Intending to exclude columns (mixing up '-' with .exclude()), or accidentally writing expr - selector instead of selector - expr.

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/eaa91dffd7062e31. Report an issue: GitHub.

Appendix: source

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

        return self.as_expr().__ror__(other)

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

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

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

    def __rsub__(self, other: Any) -> NoReturn:
        msg = "unsupported operand type(s) for op: ('Expr' - 'Selector')"
        raise TypeError(msg)

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

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

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

    def __rxor__(self, other: Any) -> Expr:
        return self.as_expr().__rxor__(other)

View on GitHub (pinned to fe841f959e)