pandas-dev/pandas · error · ValueError

Invalid Attribute context {ctx.__name__}

Error message

Invalid Attribute context {ctx.__name__}

What it means

Raised by PyTablesExprVisitor.visit_Attribute in pandas.core.computation.pytables when an AST attribute access (x.y) has a context ctx that is not ast.Load, or when attribute resolution fails to produce a usable term. The visitor handles ast.Load by resolving the value and getting the attribute; any other context (ast.Store, ast.Del, etc.) falls through to ValueError. This typically means the where expression contains an assignment or deletion targeting an attribute, which the query grammar does not support.

Source

Thrown at pandas/core/computation/pytables.py:520

        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)
            except AttributeError:
                # something like datetime.datetime where scope is overridden
                if isinstance(value, ast.Name) and value.id == attr:
                    return resolved

        raise ValueError(f"Invalid Attribute context {ctx.__name__}")

    def translate_In(self, op):
        return ast.Eq() if isinstance(op, ast.In) else op

    def _rewrite_membership_op(self, node, left, right):
        return self.visit(node.op), node.op, left, right


def _validate_where(w):
    """
    Validate that the where statement is of the right type.

    The type may either be String, Expr, or list-like of Exprs.

    Parameters
    ----------
    w : String term expression, Expr, or list-like of Exprs.

View on GitHub (pinned to 71959b8cb9)

Solutions

  1. Use only read-style attribute access in where clauses (e.g. where='index.year == 2020' for datetime index attributes).
  2. Perform assignments outside the where string: mutate the DataFrame in pandas, then write it back to the store.
  3. If you need datetime components, ensure the index/column is datetime and use supported properties (year, month, day, etc.).

Example fix

# before
store.select('df', where='a.b = 5')  # ValueError: Invalid Attribute context Store

# after (filter, don't assign)
store.select('df', where='index.year == 2020')
# mutate outside where:
df = store.get('df')
df['b'] = 5
store.put('df', df, format='table', data_columns=True)
Defensive patterns

Strategy: validation

Validate before calling

import re

def assert_no_attribute_assignment(where: str) -> str:
    if re.search(r'\w+\.\w+\s*=(?!=)', where):
        raise ValueError(f'attribute assignment is not supported in where: {where!r}')
    return where

Try / catch

try:
    store.select('df', where=where)
except ValueError as e:
    if 'Invalid Attribute context' in str(e):
        # rewrite without attribute assignment, or mutate frame in pandas
        df = store.get('df')
    else:
        raise

Prevention

When it happens

Trigger: store.select('df', where='a.b = 5') (assignment via attribute); where='del a.b'; any expression where the parser sees an attribute node in a Store/Del context. Also fires when an attribute access cannot be resolved to a term and the ctx is not Load.

Common situations: Confusing query syntax with assignment; trying to mutate columns through a where string; programmatic generation that builds ast.Attribute with the wrong ctx field.

Related errors


AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07). Data as JSON: /api/errors/e8fe5365b2bba649. Report an issue: GitHub.