pandas-dev/pandas · error · ValueError
cannot subscript {value!r} with {slobj!r}
Error message
cannot subscript {value!r} with {slobj!r} What it means
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.
Source
Thrown at pandas/core/computation/pytables.py:496
def visit_Subscript(self, node, **kwargs) -> ops.Term:
# only allow simple subscripts
value = self.visit(node.value)
slobj = self.visit(node.slice)
try:
value = value.value
except AttributeError:
pass
if isinstance(slobj, Term):
# In py39 np.ndarray lookups with Term containing int raise
slobj = slobj.value
try:
return self.const_type(value[slobj], self.env)
except TypeError as err:
raise ValueError(f"cannot subscript {value!r} with {slobj!r}") from err
def visit_Attribute(self, node, **kwargs):
attr = node.attr
value = node.value
ctx = type(node.ctx)
if ctx == ast.Load:
# resolve the value
resolved = self.visit(value)
# try to get the value to see if we are another expression
try:
resolved = resolved.value
except AttributeError:
pass
try:
return self.term_type(getattr(resolved, attr), self.env)View on GitHub (pinned to 71959b8cb9)
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.
Example fix
# before
store.select('df', where='index[df] > 5') # ValueError: cannot subscript ...
# after (precompute the key)
target = df.index[3]
store.select('df', where=f'index == {target!r}') Defensive patterns
Strategy: validation
Validate before calling
def precompute_subscript(where: str, df):
import re
# resolve simple df.index[N] -> literal value
def repl(m):
target, idx = m.group(1), int(m.group(2))
return repr(getattr(df, target)[idx])
return re.sub(r'(\w+)\[(\d+)\]', repl, where) Try / catch
try:
store.select('df', where=where)
except ValueError as e:
if 'cannot subscript' in str(e):
# precompute subscripts to literals and retry
where = precompute_subscript(where, df)
store.select('df', where=where)
else:
raise Prevention
- 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.
When it happens
Trigger: 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.
Common situations: Referencing nested data structures or attempting list/dict subscript semantics inside a where string; assuming arbitrary Python subscript syntax works in pytables expressions.
Related errors
- name {self.name!r} is not defined
- arithmetic operations are not supported inside an HDFStore '
- Cannot compare {conv_val} of type {type(conv_val)} to {kind}
- query term is not valid [{self}]
- passing a filterable condition to a non-table indexer [{self
AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07).
Data as JSON: /api/errors/d68346af43775a97.
Report an issue: GitHub.