{"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/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/computation/pytables.py#L478-L514","documentation":"Raised by PyTablesExprVisitor.visit_Subscript in pandas.core.computation.pytables when subscripting a value in a where clause raises TypeError. The visitor resolves node.value and node.slice, attempts value[slobj], and on TypeError re-raises as ValueError with both the value and the subscript object. Only simple subscripts are supported (e.g. df.index[3]); anything more elaborate (multi-axis, non-integer keys, unsupported types) trips this.","triggerScenarios":"store.select('df', where='index[df] == 5') (subscripting with a non-int); where='a[\"foo\"] == 1' (string subscript on a non-string-indexable value); where='x[1,2] == 0' (multi-axis subscript); using a column name as the subscript target that doesn't support __getitem__ with the given key.","commonSituations":"Referencing nested data structures or attempting list/dict subscript semantics inside a where string; assuming arbitrary Python subscript syntax works in pytables expressions.","solutions":["Resolve the subscript in Python first and pass the resulting scalar: idx = df.index[3]; store.select('df', where=f'index == {idx!r}').","Restrict subscripts to simple integer indexing of an existing index/Series.","If you need dict/list lookups, precompute them into a column and query that column.","Inspect the types: print(repr(value), repr(slobj)) to find what is not subscriptable."],"exampleFix":"# before\nstore.select('df', where='index[df] > 5')  # ValueError: cannot subscript ...\n\n# after (precompute the key)\ntarget = df.index[3]\nstore.select('df', where=f'index == {target!r}')","handlingStrategy":"validation","validationCode":"def precompute_subscript(where: str, df):\n    import re\n    # resolve simple df.index[N] -> literal value\n    def repl(m):\n        target, idx = m.group(1), int(m.group(2))\n        return repr(getattr(df, target)[idx])\n    return re.sub(r'(\\w+)\\[(\\d+)\\]', repl, where)","typeGuard":null,"tryCatchPattern":"try:\n    store.select('df', where=where)\nexcept ValueError as e:\n    if 'cannot subscript' in str(e):\n        # precompute subscripts to literals and retry\n        where = precompute_subscript(where, df)\n        store.select('df', where=where)\n    else:\n        raise","preventionTips":["Resolve df.index[N] / series[N] in Python before building the where string.","Use only simple integer subscripts of the index in where clauses.","Precompute nested lookups into dedicated columns."],"tags":["pandas","hdf5","pytables","where","subscript"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}