{"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/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/computation/pytables.py#L322-L358","documentation":"Raised in FilterBinOp.evaluate when self.is_valid is False at the start of evaluation. FilterBinOp represents a filterable term (used when the number of comparison values exceeds _max_selectors so PyTables uses an isin-style filter instead of an OR chain). is_valid encapsulates whether the term has a usable lhs/rhs and is_in_table state; an invalid term cannot produce a filter.","triggerScenarios":"store.select('df', where='A in [huge_list]') when the FilterBinOp was constructed from a malformed term — e.g. the lhs column is missing from queryables, or the rhs failed conversion upstream. Also from inverting or combining filters in ways that leave the op without a valid lhs.value.","commonSituations":"Querying a column that exists in the table but was not declared a data_column (degraded filter). Using a filter term after the underlying Term failed to resolve. Internal calls that build a FilterBinOp manually without setting is_valid.","solutions":["Ensure the queried column is a registered data_column: store.put('df', df, format='table', data_columns=['A']).","Reduce the filter to a simple equality/in condition that FilterBinOp can validate.","Read the data and filter in pandas: df = store.get('df'); df[df['A'].isin(values)].","Inspect the term with print(store.select_as_coordinates(...)) on a minimal condition to isolate which side is invalid."],"exampleFix":"// before\nstore.put('df', df, format='table')  # no data_columns\nstore.select('df', where='A in [1,2,3]')\n\n// after\nstore.put('df', df, format='table', data_columns=['A'])\nstore.select('df', where='A in [1,2,3]')","handlingStrategy":"validation","validationCode":"def filter_term_valid(store, key: str, column: str) -> bool:\n    storer = store.get_storer(key)\n    return column in (storer.data_columns or [])\n\nassert filter_term_valid(store, 'df', 'A'), 'A must be a data_column to use as a filter term'","typeGuard":"def is_valid_filter_term(store, key: str, column: str) -> bool:\n    storer = store.get_storer(key)\n    return column in set(storer.data_columns or [])","tryCatchPattern":"try:\n    store.select('df', where='A in huge_list')\nexcept ValueError as e:\n    if 'query term is not valid' in str(e):\n        df = store.get('df')\n        df[df['A'].isin(huge_list)]\n    else:\n        raise","preventionTips":["Declare filter columns as data_columns at write time.","Keep 'in' lists within _max_selectors when possible.","Fall back to in-memory isin for invalid filter terms."],"tags":["pandas","hdfstore","pytables","filter","where","validation"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}