{"record":{"id":"b4214346d172170f","repo":"pandas-dev/pandas","slug":"arithmetic-operations-are-not-supported-inside-an","errorCode":null,"errorMessage":"arithmetic operations are not supported inside an HDFStore 'where' filter; instead store a precomputed column as a data_column and query that, or read the data and apply the filter in pandas (e.g. df[df['A'] % 3 == 0]).","messagePattern":"arithmetic operations are not supported inside an HDFStore 'where' filter; instead store a precomputed column as a data_column and query that, or read the data and apply the filter in pandas \\(e\\.g\\. df\\[df\\['A'\\] % 3 == 0\\]\\)\\.","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"pandas/core/computation/pytables.py","lineNumber":136,"sourceCode":"    op: str\n    queryables: dict[str, Any]\n    condition: str | None\n\n    def __init__(self, op: str, lhs, rhs, queryables: dict[str, Any], encoding) -> None:\n        super().__init__(op, lhs, rhs)\n        self.queryables = queryables\n        self.encoding = encoding\n        self.condition = None\n\n    def _disallow_scalar_only_bool_ops(self) -> None:\n        pass\n\n    def prune(self, klass):\n        if self.op in ARITH_OPS_SYMS:\n            # GH#41100: arithmetic in a where-clause is not supported. PyTables\n            # support is in maintenance mode, so rather than grow the query\n            # grammar we raise with a pointer to a working alternative.\n            raise NotImplementedError(\n                \"arithmetic operations are not supported inside an HDFStore \"\n                \"'where' filter; instead store a precomputed column as a \"\n                \"data_column and query that, or read the data and apply the \"\n                \"filter in pandas (e.g. df[df['A'] % 3 == 0]).\"\n            )\n\n        def pr(left, right):\n            \"\"\"create and return a new specialized BinOp from myself\"\"\"\n            if left is None:\n                return right\n            elif right is None:\n                return left\n\n            k = klass\n            if isinstance(left, ConditionBinOp):\n                if isinstance(right, ConditionBinOp):\n                    k = JointConditionBinOp\n                elif isinstance(left, k):","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/computation/pytables.py#L118-L154","documentation":"Raised by BinOp.prune in pandas.core.computation.pytables whenever an arithmetic operator (one of ARITH_OPS_SYMS: + - * / ** // %) appears in an HDFStore 'where' clause. PyTables' on-disk query grammar only supports comparisons and boolean composition; arithmetic would require materializing the column. Per GH#41100 the team chose to raise NotImplementedError with an actionable pointer rather than expand the query grammar, since PyTables support is in maintenance mode.","triggerScenarios":"store.select('df', where='A % 3 == 0'); store.select('df', where='A + B > 10'); pd.read_hdf(path, where='price * qty > 100'). Any expression that computes a new value before comparing.","commonSituations":"Migrating SQL-like queries that compute on the fly; wanting modulo/range transforms during selection; pre-aggregation logic encoded in the where clause.","solutions":["Precompute the derived column and store it as a data_column: df['A_mod3'] = df['A'] % 3; store.put('df', df, format='table', data_columns=['A_mod3']); then store.select('df', where='A_mod3 == 0').","Read the data first and filter in pandas: df = store.get('df'); df[df['A'] % 3 == 0].","If the arithmetic is on the comparison value (not the column), move it out: precompute threshold = 3 and write where='A > @threshold' style via TermValue, or just use a literal."],"exampleFix":"# before\nstore.select('df', where='A % 3 == 0')  # NotImplementedError\n\n# after (precompute column)\ndf['A_mod3'] = df['A'] % 3\nstore.put('df', df, format='table', data_columns=['A_mod3'])\nstore.select('df', where='A_mod3 == 0')\n# after (filter in pandas)\ndf = store.get('df')\ndf[df['A'] % 3 == 0]","handlingStrategy":"validation","validationCode":"import re\nfrom pandas.core.computation.ops import ARITH_OPS_SYMS\n\ndef assert_no_arithmetic_in_where(where: str) -> str:\n    # crude check: any arithmetic op token between identifiers\n    if re.search(r'[A-Za-z_0-9\\]\\)]\\s*[+\\-*/%]|\\*\\*|//', where):\n        raise NotImplementedError(\n            f'arithmetic detected in where={where!r}; precompute the column instead'\n        )\n    return where","typeGuard":"from pandas.core.computation.ops import ARITH_OPS_SYMS\n\ndef where_has_arithmetic(where: str) -> bool:\n    return any(op in where for op in ARITH_OPS_SYMS if op != '-' or ' - ' in where)\n","tryCatchPattern":"try:\n    store.select('df', where=where)\nexcept NotImplementedError as e:\n    if 'arithmetic operations are not supported' in str(e):\n        # precompute derived column or read+filter\n        df = store.get('df')\n        result = df.query(where)\n    else:\n        raise","preventionTips":["Precompute derived columns and store them as data_columns.","Keep where clauses limited to comparisons and boolean composition.","For one-off derived filters, read the frame and filter in pandas."],"tags":["pandas","hdf5","pytables","where","arithmetic"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}