pandas-dev/pandas · error · NameError
name {self.name!r} is not defined
Error message
name {self.name!r} is not defined What it means
Raised by Term._resolve_name in pandas.core.computation.pytables when a left-hand-side identifier in an HDFStore 'where' expression is not present in env.queryables - the dict of indexable columns and data_columns the table exposes. It is raised as NameError and tells you the column does not exist or is not queryable in the table.
Source
Thrown at pandas/core/computation/pytables.py:91
class Term(ops.Term):
env: PyTablesScope
def __new__(cls, name, env, side=None, encoding=None):
if isinstance(name, str):
klass = cls
else:
klass = Constant
return object.__new__(klass)
def __init__(self, name, env: PyTablesScope, side=None, encoding=None) -> None:
super().__init__(name, env, side=side, encoding=encoding)
def _resolve_name(self):
# must be a queryables
if self.side == "left":
# Note: The behavior of __new__ ensures that self.name is a str here
if self.name not in self.env.queryables:
raise NameError(f"name {self.name!r} is not defined")
return self.name
# resolve the rhs (and allow it to be None)
try:
return self.env.resolve(self.name, is_local=False)
except UndefinedVariableError:
return self.name
# read-only property overwriting read/write property
@property # type: ignore[misc]
def value(self):
return self._value
class Constant(Term):
def __init__(self, name, env: PyTablesScope, side=None, encoding=None) -> None:
assert isinstance(env, PyTablesScope), type(env)
super().__init__(name, env, side=side, encoding=encoding)View on GitHub (pinned to 71959b8cb9)
Solutions
- Write the DataFrame with the column declared queryable: store.put('df', df, format='table', data_columns=['colname']) or data_columns=True for all columns.
- Verify the column is queryable: print(store.get_storer('df').non_index_axes) and check the data_columns list.
- Fix typos by listing available columns: print(store.get_storer('df').data_columns).
- If the column isn't queryable, read the whole frame and filter in pandas: df = store.get('df'); df[df['col'] > 5].
Example fix
# before
store.select('df', where='value > 5') # NameError if 'value' is not a data_column
# after (declare at write time)
store.put('df', df, format='table', data_columns=['value'])
store.select('df', where='value > 5')
# or filter in pandas:
df = store.get('df')
df[df['value'] > 5] Defensive patterns
Strategy: validation
Validate before calling
def assert_queryable(store, key, column):
storer = store.get_storer(key)
queryable = list((storer.data_columns or [])) + (storer.index_axes or [])
if column not in [getattr(a, 'name', None) for a in queryable]:
raise NameError(f'{column!r} is not queryable; declare it as a data_column') Type guard
def is_queryable_column(store, key, column) -> bool:
try:
storer = store.get_storer(key)
names = [getattr(a, 'name', None) for a in (storer.data_columns or [])]
names += [getattr(a, 'name', None) for a in (storer.index_axes or [])]
return column in names
except Exception:
return False
Try / catch
try:
store.select('df', where=f'{col} > 5')
except NameError as e:
if 'is not defined' in str(e):
# read all and filter in pandas
df = store.get('df')
result = df[df[col] > 5]
else:
raise Prevention
- Declare data_columns at write time for any column you intend to query.
- Verify column names with store.get_storer(key).data_columns before selecting.
- Use store.keys() to confirm the correct group/key.
When it happens
Trigger: store.select('df', where='nonexistent_col > 5'); pd.read_hdf(path, 'df', where='missing == 3'); querying a column that exists in the DataFrame but was not declared as a data_column when written to HDF5.
Common situations: Writing a DataFrame to HDF5 without data_columns=True (only the index is queryable by default); typos in column names; assuming all columns are queryable; schema drift between writer and reader (column renamed/removed).
Related errors
- 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
- unable to collapse Joint Filters
AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07).
Data as JSON: /api/errors/25a75872595cf839.
Report an issue: GitHub.