pandas-dev/pandas · error · ValueError
cannot assign without a target object
Error message
cannot assign without a target object
What it means
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.
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={}.
Example fix
// before
pd.eval('c = a + b')
// after
df.eval('c = a + b')
// or
pd.eval('c = a + b', target=df) Defensive patterns
Strategy: validation
Validate before calling
has_assign = isinstance(expr, str) and '=' in expr
if has_assign and target is None:
raise ValueError('Assignment expressions require a target — pass target=df or use df.eval')
pd.eval(expr, target=target) Try / catch
try:
pd.eval(expr)
except ValueError as e:
if 'cannot assign without a target' in str(e):
result = df.eval(expr)
else:
raise Prevention
- Use df.eval for assignment expressions.
- Pass target= when using pd.eval with '='.
- Detect '=' in expr before calling pd.eval.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Cannot assign expression output to target
- Cannot return a copy of the target
- Multi-line expressions are only valid if all expressions…
- can only assign a single expression
- Cannot operate inplace if there is no assignment
AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11).
Data as JSON: /api/errors/ad5c0bc6b8c8942e.
Report an issue: GitHub.
Appendix: source
Thrown at pandas/core/computation/expr.py:628
return slice(lower, upper, step)
def visit_Assign(self, node, **kwargs):
"""
support a single assignment node, like
c = a + b
set the assigner at the top level, must be a Name node which
might or might not exist in the resolvers
"""
if len(node.targets) != 1:
raise SyntaxError("can only assign a single expression")
if not isinstance(node.targets[0], ast.Name):
raise SyntaxError("left hand side of an assignment must be a single name")
if self.env.target is None:
raise ValueError("cannot assign without a target object")
try:
assigner = self.visit(node.targets[0], **kwargs)
except UndefinedVariableError:
assigner = node.targets[0].id
self.assigner = getattr(assigner, "name", assigner)
if self.assigner is None:
raise SyntaxError(
"left hand side of an assignment must be a single resolvable name"
)
return self.visit(node.value, **kwargs)
def visit_Attribute(self, node, **kwargs):
attr = node.attr
value = node.value
View on GitHub (pinned to 3b7651241d)