pandas-dev/pandas · error · ValueError

Multi-line expressions are only valid if all expressions…

Error message

Multi-line expressions are only valid if all expressions contain an assignment

What it means

Raised after evaluation when parsed_expr.assigner is None (i.e., the expression has no top-level assignment) but multi_line is True. In a multi-line expression every line must contain an assignment (e.g., 'c = a + b'); a bare expression line like 'a + 2' has no assigner, so the multi-line contract is violated. This catches inconsistent multi-line input where some lines assign and some do not.

Solutions

  1. Ensure every line in a multi-line eval contains an assignment: df.eval('a = b + 1\nc = a + 2').
  2. If you need the final value returned, split into separate calls: assign all columns, then read the result separately.
  3. Remove the non-assigning line if it was unintentional.

Example fix

// before
df.eval('a = b + 1\na + 2')

// after
df.eval('a = b + 1\nc = a + 2')
Defensive patterns

Strategy: validation

Validate before calling

import re
def all_lines_assign(expr: str) -> bool:
    lines = [l.strip() for l in expr.splitlines() if l.strip()]
    return all(re.search(r'^[A-Za-z_]\w*\s*=', l) for l in lines)

if not all_lines_assign(expr):
    raise ValueError('Every line must contain an assignment')
df.eval(expr)

Try / catch

try:
    df.eval(expr)
except ValueError as e:
    if 'must contain an assignment' in str(e):
        # rewrite or split lines
        ...

Prevention

When it happens

Trigger: Calling df.eval('a = b + 1\na + 2') — the second line 'a + 2' has no assignment, so assigner is None and multi_line is True.

Common situations: Building a multi-line expression programmatically and appending a final 'return' or display line that is a bare expression; copy-pasting a sequence of computations where the last line was meant as output rather than an assignment.

Related errors


AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11). Data as JSON: /api/errors/65f07ed9f466e0f9. Report an issue: GitHub.

Appendix: source

Thrown at pandas/core/computation/eval.py:404

                )
            )
        ):
            warnings.warn(
                "Engine has switched to 'python' because numexpr does not support "
                "extension array dtypes. Please set your engine to python manually.",
                RuntimeWarning,
                stacklevel=find_stack_level(),
            )
            engine = "python"

        # construct the engine and evaluate the parsed expression
        eng = ENGINES[engine]
        eng_inst = eng(parsed_expr)
        ret = eng_inst.evaluate()

        if parsed_expr.assigner is None:
            if multi_line:
                raise ValueError(
                    "Multi-line expressions are only valid "
                    "if all expressions contain an assignment"
                )
            if inplace:
                raise ValueError("Cannot operate inplace if there is no assignment")

        # assign if needed
        assigner = parsed_expr.assigner
        if env.target is not None and assigner is not None:
            target_modified = True

            # if returning a copy, copy only on the first assignment
            if not inplace and first_expr:
                try:
                    target = env.target
                    if isinstance(target, NDFrame):
                        target = target.copy(deep=False)
                    else:

View on GitHub (pinned to 3b7651241d)