{"record":{"id":"ad5c0bc6b8c8942e","repo":"pandas-dev/pandas","slug":"cannot-assign-without-a-target-object","errorCode":null,"errorMessage":"cannot assign without a target object","messagePattern":"cannot assign without a target object","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/computation/expr.py","lineNumber":628,"sourceCode":"\n        return slice(lower, upper, step)\n\n    def visit_Assign(self, node, **kwargs):\n        \"\"\"\n        support a single assignment node, like\n\n        c = a + b\n\n        set the assigner at the top level, must be a Name node which\n        might or might not exist in the resolvers\n\n        \"\"\"\n        if len(node.targets) != 1:\n            raise SyntaxError(\"can only assign a single expression\")\n        if not isinstance(node.targets[0], ast.Name):\n            raise SyntaxError(\"left hand side of an assignment must be a single name\")\n        if self.env.target is None:\n            raise ValueError(\"cannot assign without a target object\")\n\n        try:\n            assigner = self.visit(node.targets[0], **kwargs)\n        except UndefinedVariableError:\n            assigner = node.targets[0].id\n\n        self.assigner = getattr(assigner, \"name\", assigner)\n        if self.assigner is None:\n            raise SyntaxError(\n                \"left hand side of an assignment must be a single resolvable name\"\n            )\n\n        return self.visit(node.value, **kwargs)\n\n    def visit_Attribute(self, node, **kwargs):\n        attr = node.attr\n        value = node.value\n","sourceCodeStart":610,"sourceCodeEnd":646,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/computation/expr.py#L610-L646","documentation":"Raised by visit_Assign() when an assignment expression is encountered but self.env.target is None — i.e., there is no target object to assign into. This is the parser-level counterpart of the eval-level checks; it fires when the expression contains '=' (assignment) but no target was provided to the eval call. The target is what receives the new column/value.","triggerScenarios":"Calling pd.eval('c = a + b') without target= (target defaults to None). The expression contains an assignment but there is nowhere to put the result.","commonSituations":"Using pd.eval instead of df.eval for an assignment expression; forgetting to pass target when using the top-level eval function with an assignment.","solutions":["Use DataFrame.eval for column assignments: df.eval('c = a + b').","If using pd.eval, pass a target: pd.eval('c = a + b', target=df) or target={}."],"exampleFix":"// before\npd.eval('c = a + b')\n\n// after\ndf.eval('c = a + b')\n// or\npd.eval('c = a + b', target=df)","handlingStrategy":"validation","validationCode":"has_assign = isinstance(expr, str) and '=' in expr\nif has_assign and target is None:\n    raise ValueError('Assignment expressions require a target — pass target=df or use df.eval')\npd.eval(expr, target=target)","typeGuard":null,"tryCatchPattern":"try:\n    pd.eval(expr)\nexcept ValueError as e:\n    if 'cannot assign without a target' in str(e):\n        result = df.eval(expr)\n    else:\n        raise","preventionTips":["Use df.eval for assignment expressions.","Pass target= when using pd.eval with '='.","Detect '=' in expr before calling pd.eval."],"tags":["pandas","eval","assignment","target","validation"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}