{"record":{"id":"d68346af43775a97","repo":"pandas-dev/pandas","slug":"cannot-subscript-value-r-with-slobj-r","errorCode":null,"errorMessage":"cannot subscript {value!r} with {slobj!r}","messagePattern":"cannot subscript (.+?) with (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/computation/pytables.py","lineNumber":496,"sourceCode":"\n    def visit_Subscript(self, node, **kwargs) -> ops.Term:\n        # only allow simple subscripts\n\n        value = self.visit(node.value)\n        slobj = self.visit(node.slice)\n        try:\n            value = value.value\n        except AttributeError:\n            pass\n\n        if isinstance(slobj, Term):\n            # In py39 np.ndarray lookups with Term containing int raise\n            slobj = slobj.value\n\n        try:\n            return self.const_type(value[slobj], self.env)\n        except TypeError as err:\n            raise ValueError(f\"cannot subscript {value!r} with {slobj!r}\") from err\n\n    def visit_Attribute(self, node, **kwargs):\n        attr = node.attr\n        value = node.value\n\n        ctx = type(node.ctx)\n        if ctx == ast.Load:\n            # resolve the value\n            resolved = self.visit(value)\n\n            # try to get the value to see if we are another expression\n            try:\n                resolved = resolved.value\n            except AttributeError:\n                pass\n\n            try:\n                return self.term_type(getattr(resolved, attr), self.env)","sourceCodeStart":478,"sourceCodeEnd":514,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/computation/pytables.py#L478-L514","documentation":"Raised in PyTablesExprVisitor.visit_Subscript when value[slobj] raises TypeError. The visitor only supports 'simple subscripts' — indexing into a Term's value (e.g. a stored constant or attribute) with an integer/Term scalar. If the slice/index type is incompatible with the value's __getitem__ (e.g. indexing a scalar, a non-subscriptable object, or using a multi-dim slice), Python raises TypeError and the visitor re-wraps it as ValueError with both operands in the message.","triggerScenarios":"store.select('df', where='A[\"bad\"] > 0') (string index on a numeric column), store.select('df', where='A[1:2] > 0') (slice not allowed as 'simple subscript'), or subscripting a scalar term: store.select('df', where='5[0] > 0'). Also from referencing a non-subscriptable @-local inside the where clause.","commonSituations":"Using list/dict-style indexing syntax in a where clause. Trying to slice a column inside an HDFStore select. Confusing query subscript support with pandas .loc/.iloc semantics.","solutions":["Move the subscript out of the where clause: precompute the indexed value in Python and pass it as a @-local or store it as a data_column.","Use only integer scalar subscripts if subscripting is unavoidable (e.g. on a stored constant term).","Read the data and use pandas indexing: df = store.get('df'); df.loc[df.index[1:2], 'A'].","Rewrite slice conditions as range comparisons: 'A > 5' instead of indexing."],"exampleFix":"// before\nstore.select('df', where='A[\"x\"] > 0')\nstore.select('df', where='A[1:3] > 0')\n\n// after\nval = some_lookup['x']\nstore.select('df', where='A == @val')\n# for ranges\nstore.select('df', where='A > 5 & A < 10')","handlingStrategy":"validation","validationCode":"import re\n\ndef where_has_complex_subscript(where: str) -> bool:\n    # detect subscript with slice or non-integer index\n    return bool(re.search(r'\\[\\s*[^\\]0-9@]+', where))\n\nassert not where_has_complex_subscript(where_clause)","typeGuard":"def is_simple_integer_subscript(slobj) -> bool:\n    return isinstance(slobj, int)","tryCatchPattern":"try:\n    store.select('df', where='A[\"x\"] > 0')\nexcept ValueError as e:\n    if 'cannot subscript' in str(e):\n        df = store.get('df')\n        df[df['A'] == 'x_value']\n    else:\n        raise","preventionTips":["Avoid list/dict-style indexing inside where clauses.","Precompute indexed lookups in Python and inject via @-locals or data_columns.","Use range comparisons instead of slice subscripts."],"tags":["pandas","hdfstore","pytables","subscript","where","indexing"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}