pandas-dev/pandas · error · NotImplementedError

cannot use an invert condition when passing to numexpr

Error message

cannot use an invert condition when passing to numexpr

What it means

Raised in ConditionBinOp.invert. ConditionBinOp produces a numexpr condition string (e.g. '(A > 5)'); numexpr/PyTables condition inversion via Python '~' is not wired up, so attempting to invert a condition — typically through a '~(...)' wrapper in the where clause routed through the numexpr engine — raises NotImplementedError. The commented-out code shows the historical attempt to wrap the condition in '~(...)'.

Solutions

  1. Rewrite the negation as a positive condition: store.select('df', where='A <= 5') instead of '~(A > 5)'.
  2. Use De Morgan's laws to push the negation onto comparisons: '~(A > 5 & B < 3)' -> '(A <= 5 | B >= 3)'.
  3. Read the data and filter in pandas if rewriting is impractical.

Example fix

// before
store.select('df', where='~(A > 5)')

// after
store.select('df', where='A <= 5')
Defensive patterns

Strategy: validation

Validate before calling

import re

def where_has_invert(where: str) -> bool:
    return bool(re.search(r'~\s*\(', where))

assert not where_has_invert(where_clause), 'invert (~) conditions are not supported via numexpr in HDFStore'

Type guard

def is_positive_predicate(op: str) -> bool:
    return op in ('>', '<', '>=', '<=', '==', '!=', 'in', 'not in')

Try / catch

try:
    store.select('df', where='~(A > 5)')
except NotImplementedError as e:
    if 'invert condition' in str(e):
        store.select('df', where='A <= 5')
    else:
        raise

Prevention

When it happens

Trigger: store.select('df', where='~(A > 5)') when the condition is compiled for numexpr. Also from explicit .invert() calls on a ConditionBinOp during query pruning, or using 'not' inside a where clause that resolves to a numexpr condition.

Common situations: Negating a where clause expecting SQL-style NOT. Porting df[~(df.A > 5)] to HDFStore select. Mixing '~' with numexpr-routed conditions.

Related errors


AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11). Data as JSON: /api/errors/298a4db0d687916a. Report an issue: GitHub.

Appendix: source

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

class JointFilterBinOp(FilterBinOp):
    def format(self):
        raise NotImplementedError("unable to collapse Joint Filters")

    # error: Signature of "evaluate" incompatible with supertype "BinOp"
    def evaluate(self) -> Self:  # type: ignore[override]
        return self


class ConditionBinOp(BinOp):
    def __repr__(self) -> str:
        return pprint_thing(f"[Condition : [{self.condition}]]")

    def invert(self):
        """invert the condition"""
        # if self.condition is not None:
        #    self.condition = "~(%s)" % self.condition
        # return self
        raise NotImplementedError(
            "cannot use an invert condition when passing to numexpr"
        )

    def format(self):
        """return the actual ne format"""
        return self.condition

    # error: Signature of "evaluate" incompatible with supertype "BinOp"
    def evaluate(self) -> Self | None:  # type: ignore[override]
        if not self.is_valid:
            raise ValueError(f"query term is not valid [{self}]")

        # convert values if we are in the table
        if not self.is_in_table:
            return None

        rhs = self.conform(self.rhs)
        values = [self.convert_value(v) for v in rhs]

View on GitHub (pinned to 3b7651241d)