pandas-dev/pandas · error · NotImplementedError

UnaryOp only support invert type ops

Error message

UnaryOp only support invert type ops

What it means

Raised by UnaryOp.prune in pandas.core.computation.pytables when the unary operator is not '~'. The pytables visitor only supports invert ('~' / 'not') as a unary op in where clauses; '+' and '-' are handled earlier in visit_UnaryOp (USub becomes a negated constant, UAdd raises separately). If any other unary form reaches pruning, this NotImplementedError fires.

Source

Thrown at pandas/core/computation/pytables.py:429

            else:
                return None
        else:
            self.condition = self.generate(values[0])

        return self


class JointConditionBinOp(ConditionBinOp):
    # error: Signature of "evaluate" incompatible with supertype "BinOp"
    def evaluate(self) -> Self:  # type: ignore[override]
        self.condition = f"({self.lhs.condition} {self.op} {self.rhs.condition})"
        return self


class UnaryOp(ops.UnaryOp):
    def prune(self, klass):
        if self.op != "~":
            raise NotImplementedError("UnaryOp only support invert type ops")

        operand = self.operand
        operand = operand.prune(klass)

        if operand is not None and (
            (issubclass(klass, ConditionBinOp) and operand.condition is not None)
            or (
                not issubclass(klass, ConditionBinOp)
                and issubclass(klass, FilterBinOp)
                and operand.filter is not None
            )
        ):
            return operand.invert()
        return None


class PyTablesExprVisitor(BaseExprVisitor):
    const_type: ClassVar[type[ops.Term]] = Constant

View on GitHub (pinned to 71959b8cb9)

Solutions

  1. Use only '~' (or 'not') for unary negation in where clauses: where='~(a > 0)'.
  2. For arithmetic negation, precompute the column (df['neg_a'] = -df['a']) and store as a data_column, then query that.
  3. Avoid constructing UnaryOp manually; rely on the parser via PyTablesExpr/where strings.

Example fix

# before
store.select('df', where='-a > 0')  # may raise UnaryOp only support invert type ops after remap

# after (precompute)
df['neg_a'] = -df['a']
store.put('df', df, format='table', data_columns=['neg_a'])
store.select('df', where='neg_a > 0')
Defensive patterns

Strategy: validation

Validate before calling

def assert_invert_only_unary(op: str) -> str:
    if op != '~':
        raise NotImplementedError(f'pytables UnaryOp only supports ~, got {op!r}')
    return op

Type guard

def is_invert_unary(op: str) -> bool:
    return op == '~'

Try / catch

try:
    store.select('df', where=where)
except NotImplementedError as e:
    if 'UnaryOp only support invert' in str(e):
        df = store.get('df')
        result = df.query(where)
    else:
        raise

Prevention

When it happens

Trigger: Constructing a pytables UnaryOp directly with op in ('+','-','not' but mistyped), or a custom AST path that yields a non-invert unary in a where clause. In normal where strings, '+' and '-' are intercepted before prune, so this is largely defensive.

Common situations: Custom subclasses of the pytables visitor; experimental AST manipulation; using 'not' in a context that bypasses visit_UnaryOp's remap.

Related errors


AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07). Data as JSON: /api/errors/6b4d1a0dafbc4c64. Report an issue: GitHub.