{"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/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/computation/pytables.py#L118-L154","documentation":"Raised in the PyTables BinOp.prune when self.op is in ARITH_OPS_SYMS ('+', '-', '*', '/', '**', '//', '%'). HDFStore 'where' filters are translated into PyTables/numexpr conditions on stored data_columns; arithmetic inside the condition would require computing new columns on disk, which PyTables does not support. Per GH#41100, rather than grow the query grammar the team chose to raise NotImplementedError with a pointer to alternatives, since PyTables support is in maintenance mode.","triggerScenarios":"store.select('df', where='A + B > 1'), store.select('df', where='A % 3 == 0'), or any arithmetic operator inside the where clause. Also df.query() evaluated against an HDFStore-backed TermVisitor.","commonSituations":"Porting a pandas boolean expression (df[df['A'] + df['B'] > 1]) to an HDFStore select without precomputing. Expecting numexpr/PyTables to evaluate derived expressions. Trying to filter on a ratio (A/B) without a stored column.","solutions":["Precompute the arithmetic result into a new column and store it as a data_column: df['A_plus_B'] = df['A'] + df['B']; store.put('df', df, format='table', data_columns=['A_plus_B']); store.select('df', where='A_plus_B > 1').","Read the data and filter in pandas: df = store.get('df'); df[df['A'] + df['B'] > 1].","Avoid HDFStore where filters for derived expressions; use Parquet + boolean indexing for new code."],"exampleFix":"// before\nstore.select('df', where='A + B > 1')\n\n// after\ndf['A_plus_B'] = df['A'] + df['B']\nstore.put('df', df, format='table', data_columns=['A_plus_B'])\nstore.select('df', where='A_plus_B > 1')\n# or simply\ndf = store.get('df')\ndf[df['A'] + df['B'] > 1]","handlingStrategy":"validation","validationCode":"from pandas.core.computation.ops import ARITH_OPS_SYMS\nimport re\n\ndef where_has_arith(where: str) -> bool:\n    # crude check: any arithmetic operator token outside string literals\n    return bool(re.search(r'[+\\-*/%]|\\*\\*|//', where))\n\nassert not where_has_arith(where_clause), 'arithmetic is not allowed in HDFStore where clauses'","typeGuard":"from pandas.core.computation.ops import ARITH_OPS_SYMS\n\ndef is_arith_op(op: str) -> bool:\n    return op in ARITH_OPS_SYMS","tryCatchPattern":"try:\n    store.select('df', where='A + B > 1')\nexcept NotImplementedError as e:\n    if 'arithmetic operations are not supported' in str(e):\n        df = store.get('df')\n        df[(df['A'] + df['B']) > 1]\n    else:\n        raise","preventionTips":["Precompute derived columns and store them as data_columns.","Read fixed/complex predicates into pandas and filter in memory.","Reject arithmetic tokens in where-clause builders."],"tags":["pandas","hdfstore","pytables","where","arithmetic","maintenance-mode"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}