{"record":{"id":"bd3275918ba99e3a","repo":"pandas-dev/pandas","slug":"query-term-is-not-valid-self","errorCode":null,"errorMessage":"query term is not valid [{self}]","messagePattern":"query term is not valid \\[(.+?)\\]","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/computation/pytables.py","lineNumber":340,"sourceCode":"\n    def invert(self) -> Self:\n        \"\"\"invert the filter\"\"\"\n        if self.filter is not None:\n            self.filter = (\n                self.filter[0],\n                self.generate_filter_op(invert=True),\n                self.filter[2],\n            )\n        return self\n\n    def format(self):\n        \"\"\"return the actual filter format\"\"\"\n        return [self.filter]\n\n    # error: Signature of \"evaluate\" incompatible with supertype \"BinOp\"\n    def evaluate(self) -> Self | None:  # type: ignore[override]\n        if not self.is_valid:\n            raise ValueError(f\"query term is not valid [{self}]\")\n\n        rhs = self.conform(self.rhs)\n        values = list(rhs)\n\n        if self.op not in [\"==\", \"!=\"]:\n            if not self.is_in_table:\n                raise TypeError(\n                    f\"passing a filterable condition to a non-table indexer [{self}]\"\n                )\n            return None\n\n        if self.is_in_table and len(values) <= self._max_selectors:\n            return None\n        filter_op = self.generate_filter_op()\n        self.filter = (self.lhs.value, filter_op, Index(values))\n        return self\n\n    def generate_filter_op(self, invert: bool = False):","sourceCodeStart":322,"sourceCodeEnd":358,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/computation/pytables.py#L322-L358","documentation":"Raised by FilterBinOp.evaluate in pandas.core.computation.pytables when self.is_valid is False - meaning the left-hand side of the operator is not present in env.queryables. is_valid checks `self.lhs.value in self.queryables`. This is the filter-path counterpart to the NameError at pytables.py:91: the column name does not resolve to a queryable field. It is a ValueError and includes the offending term in [{self}].","triggerScenarios":"store.select('df', where='unknown_col == [1,2,3]') where 'unknown_col' is not a data_column or index. Also when an equality list is built against a column that was never declared queryable.","commonSituations":"Schema mismatch between writer and reader; column renamed; querying a column that exists in the frame but was not stored as a data_column; copy-paste errors in the where string.","solutions":["Declare the column as a data_column at write time: store.put('df', df, format='table', data_columns=['colname']).","List available queryables to confirm spelling: print(store.get_storer('df').data_columns).","If you only need filtering once, read the frame and filter in pandas: df[df['col'].isin([1,2,3])].","Check whether you are querying the correct key/group inside the HDF file (store.keys())."],"exampleFix":"# before\nstore.select('df', where='city == [\"NYC\", \"LA\"]')  # ValueError: query term is not valid\n\n# after\ndf.to_hdf(path, 'df', format='table', data_columns=['city'])\nstore.select('df', where='city == [\"NYC\", \"LA\"]')","handlingStrategy":"validation","validationCode":"def assert_filter_column(store, key, column):\n    storer = store.get_storer(key)\n    data_cols = list(storer.data_columns or [])\n    idx_cols = [getattr(a, 'name', None) for a in (storer.index_axes or [])]\n    if column not in data_cols + idx_cols:\n        raise ValueError(f'{column!r} not queryable; data_columns={data_cols}')","typeGuard":"def is_filterable_column(store, key, column) -> bool:\n    try:\n        storer = store.get_storer(key)\n        return column in (storer.data_columns or [])\n    except Exception:\n        return False\n","tryCatchPattern":"try:\n    store.select('df', where=f'{col} == [1,2,3]')\nexcept ValueError as e:\n    if 'query term is not valid' in str(e):\n        df = store.get('df')\n        result = df[df[col].isin([1, 2, 3])]\n    else:\n        raise","preventionTips":["Declare list-membership columns as data_columns at write time.","Validate column names against store.get_storer(key).data_columns.","Fall back to df[df[col].isin([...])] for ad-hoc filtering."],"tags":["pandas","hdf5","pytables","where","validation"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}