pandas-dev/pandas · error · SyntaxError
can only assign a single expression
Error message
can only assign a single expression
What it means
Raised by visit_Assign() when the assignment node has more than one target (len(node.targets) != 1). pandas eval supports only a single assignment target like 'c = a + b'; chained or multiple-target assignments such as 'a = b = 1' or tuple unpacking 'a, b = 1, 2' are rejected because the assigner model tracks exactly one name.
Solutions
- Split into one assignment per line: df.eval('a = 1\nb = 1').
- Assign each target in a separate df.eval call or via regular DataFrame column assignment df['a'] = 1; df['b'] = 1.
Example fix
// before
df.eval('a = b = 1')
// after
df.eval('a = 1\nb = 1') Defensive patterns
Strategy: validation
Validate before calling
import re
def is_single_target_assign(expr: str) -> bool:
# reject chained 'a = b ='
return expr.count('=') == 1
if not is_single_target_assign(expr):
raise ValueError('Use one assignment target per line')
df.eval(expr) Try / catch
try:
df.eval(expr)
except SyntaxError as e:
if 'single expression' in str(e):
# split chained assignments
... Prevention
- One assignment target per eval line.
- Use multiple df.eval calls or multi-line for several columns.
- Avoid Python chained-assignment idioms in eval.
When it happens
Trigger: df.eval('a = b = 1') (chained assignment); df.eval('a, b = 1, 2') (tuple unpacking, though this may hit a different error first); any expression with multiple '=' targets in one statement.
Common situations: Trying to initialize multiple columns in one eval line; porting Python multiple-assignment idioms into the eval DSL.
Related errors
- left hand side of an assignment must be a single name
- left hand side of an assignment must be a single resolvable…
- Cannot assign expression output to target
- cannot assign without a target object
- Cannot operate inplace if there is no assignment
AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11).
Data as JSON: /api/errors/c21a70eddc9a268a.
Report an issue: GitHub.
Appendix: source
Thrown at pandas/core/computation/expr.py:624
upper = self.visit(upper).value
step = node.step
if step is not None:
step = self.visit(step).value
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)
View on GitHub (pinned to 3b7651241d)