{"record":{"id":"e8fe5365b2bba649","repo":"pandas-dev/pandas","slug":"invalid-attribute-context-ctx-name","errorCode":null,"errorMessage":"Invalid Attribute context {ctx.__name__}","messagePattern":"Invalid Attribute context (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/computation/pytables.py","lineNumber":520,"sourceCode":"        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)\n            except AttributeError:\n                # something like datetime.datetime where scope is overridden\n                if isinstance(value, ast.Name) and value.id == attr:\n                    return resolved\n\n        raise ValueError(f\"Invalid Attribute context {ctx.__name__}\")\n\n    def translate_In(self, op):\n        return ast.Eq() if isinstance(op, ast.In) else op\n\n    def _rewrite_membership_op(self, node, left, right):\n        return self.visit(node.op), node.op, left, right\n\n\ndef _validate_where(w):\n    \"\"\"\n    Validate that the where statement is of the right type.\n\n    The type may either be String, Expr, or list-like of Exprs.\n\n    Parameters\n    ----------\n    w : String term expression, Expr, or list-like of Exprs.\n","sourceCodeStart":502,"sourceCodeEnd":538,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/computation/pytables.py#L502-L538","documentation":"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.","triggerScenarios":"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.","commonSituations":"Confusing query syntax with assignment; trying to mutate columns through a where string; programmatic generation that builds ast.Attribute with the wrong ctx field.","solutions":["Use only read-style attribute access in where clauses (e.g. where='index.year == 2020' for datetime index attributes).","Perform assignments outside the where string: mutate the DataFrame in pandas, then write it back to the store.","If you need datetime components, ensure the index/column is datetime and use supported properties (year, month, day, etc.)."],"exampleFix":"# before\nstore.select('df', where='a.b = 5')  # ValueError: Invalid Attribute context Store\n\n# after (filter, don't assign)\nstore.select('df', where='index.year == 2020')\n# mutate outside where:\ndf = store.get('df')\ndf['b'] = 5\nstore.put('df', df, format='table', data_columns=True)","handlingStrategy":"validation","validationCode":"import re\n\ndef assert_no_attribute_assignment(where: str) -> str:\n    if re.search(r'\\w+\\.\\w+\\s*=(?!=)', where):\n        raise ValueError(f'attribute assignment is not supported in where: {where!r}')\n    return where","typeGuard":null,"tryCatchPattern":"try:\n    store.select('df', where=where)\nexcept ValueError as e:\n    if 'Invalid Attribute context' in str(e):\n        # rewrite without attribute assignment, or mutate frame in pandas\n        df = store.get('df')\n    else:\n        raise","preventionTips":["Never use assignment (=) or del inside a where clause.","Use only read-style attribute access (e.g. index.year).","Perform mutations on the DataFrame in pandas, then write back."],"tags":["pandas","hdf5","pytables","where","attribute","ast"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}