pandas-dev/pandas · error · ValueError
cannot subscript with
Error message
cannot subscript {value!r} with {slobj!r} What it means
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.
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.
Example fix
// before
store.select('df', where='A["x"] > 0')
store.select('df', where='A[1:3] > 0')
// after
val = some_lookup['x']
store.select('df', where='A == @val')
# for ranges
store.select('df', where='A > 5 & A < 10') Defensive patterns
Strategy: validation
Validate before calling
import re
def where_has_complex_subscript(where: str) -> bool:
# detect subscript with slice or non-integer index
return bool(re.search(r'\[\s*[^\]0-9@]+', where))
assert not where_has_complex_subscript(where_clause) Type guard
def is_simple_integer_subscript(slobj) -> bool:
return isinstance(slobj, int) Try / catch
try:
store.select('df', where='A["x"] > 0')
except ValueError as e:
if 'cannot subscript' in str(e):
df = store.get('df')
df[df['A'] == 'x_value']
else:
raise Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- arithmetic operations are not supported inside an HDFStore…
- Cannot compare of type to column
- cannot use an invert condition when passing to numexpr
- name is not defined
- passing a filterable condition to a non-table indexer
AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11).
Data as JSON: /api/errors/d68346af43775a97.
Report an issue: GitHub.
Appendix: 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 3b7651241d)