pandas-dev/pandas · error · ValueError

Cannot operate inplace if there is no assignment

Error message

Cannot operate inplace if there is no assignment

What it means

Raised after evaluation when parsed_expr.assigner is None (no assignment in the expression) but inplace=True was requested. Inplace operation only makes sense when there is a column/variable to mutate; without an assignment target inside the expression, there is nothing to operate on inplace.

Solutions

  1. Add an assignment to the expression: df.eval('c = a + b', inplace=True).
  2. Drop inplace=True if you only want the computed value: result = df.eval('a + b').

Example fix

// before
df.eval('a + b', inplace=True)

// after
df.eval('c = a + b', inplace=True)
Defensive patterns

Strategy: validation

Validate before calling

has_assignment = isinstance(expr, str) and '=' in expr.splitlines()[0]
if inplace and not has_assignment:
    raise ValueError('inplace=True requires an assignment expression')
df.eval(expr, inplace=inplace)

Try / catch

try:
    df.eval(expr, inplace=True)
except ValueError as e:
    if 'no assignment' in str(e):
        result = df.eval(expr, inplace=False)
    else:
        raise

Prevention

When it happens

Trigger: Calling df.eval('a + b', inplace=True) — the expression 'a + b' produces a value but does not assign it, so inplace has no effect and is rejected.

Common situations: Setting inplace=True globally or in a wrapper that always passes it; misunderstanding that inplace applies to the assignment target inside the expression, not to returning a value.

Related errors


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

Appendix: source

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

                "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:
                        target = target.copy()
                except AttributeError as err:
                    raise ValueError("Cannot return a copy of the target") from err
            else:
                target = env.target

View on GitHub (pinned to 3b7651241d)