{"record":{"id":"5529d192132d5448","repo":"pandas-dev/pandas","slug":"cannot-operate-inplace-if-there-is-no-assignment","errorCode":null,"errorMessage":"Cannot operate inplace if there is no assignment","messagePattern":"Cannot operate inplace if there is no assignment","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/computation/eval.py","lineNumber":409,"sourceCode":"                \"extension array dtypes. Please set your engine to python manually.\",\n                RuntimeWarning,\n                stacklevel=find_stack_level(),\n            )\n            engine = \"python\"\n\n        # construct the engine and evaluate the parsed expression\n        eng = ENGINES[engine]\n        eng_inst = eng(parsed_expr)\n        ret = eng_inst.evaluate()\n\n        if parsed_expr.assigner is None:\n            if multi_line:\n                raise ValueError(\n                    \"Multi-line expressions are only valid \"\n                    \"if all expressions contain an assignment\"\n                )\n            if inplace:\n                raise ValueError(\"Cannot operate inplace if there is no assignment\")\n\n        # assign if needed\n        assigner = parsed_expr.assigner\n        if env.target is not None and assigner is not None:\n            target_modified = True\n\n            # if returning a copy, copy only on the first assignment\n            if not inplace and first_expr:\n                try:\n                    target = env.target\n                    if isinstance(target, NDFrame):\n                        target = target.copy(deep=False)\n                    else:\n                        target = target.copy()\n                except AttributeError as err:\n                    raise ValueError(\"Cannot return a copy of the target\") from err\n            else:\n                target = env.target","sourceCodeStart":391,"sourceCodeEnd":427,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/computation/eval.py#L391-L427","documentation":"inplace=True tells pandas to mutate the target rather than return a copy. With no assignment in the expression (parsed_expr.assigner is None at eval.py:402), there is nothing to mutate, so inplace is meaningless and pandas refuses rather than silently no-op. The check sits in the assigner-is-None branch alongside the multi-line check.","triggerScenarios":"df.eval('a + b', inplace=True), df.query('a > b', inplace=True) — any read-only expression combined with inplace=True.","commonSituations":"Setting inplace=True globally by habit or copy-paste. Confusing query (which filters rows) with assignment semantics. Expecting inplace to mean 'cache the result'.","solutions":["Drop inplace=True for read-only expressions and use the returned Series/frame.","Add an assignment to make the expression mutative: df.eval('c = a + b', inplace=True).","Use df.loc or direct column assignment if you want to store the result."],"exampleFix":"// before\ndf.eval('a + b', inplace=True)\n// after\ndf.eval('c = a + b', inplace=True)","handlingStrategy":"validation","validationCode":"def validate_inplace_has_assignment(expr: str, inplace: bool) -> None:\n    import ast\n    if inplace:\n        tree = ast.parse(expr, mode='exec')\n        if not any(isinstance(n, ast.Assign) for n in tree.body):\n            raise ValueError(\n                'inplace=True requires an assignment in the expression'\n            )\n\nvalidate_inplace_has_assignment(expr, inplace)","typeGuard":"import ast\n\ndef inplace_is_safe(expr: str, inplace: bool) -> bool:\n    if not inplace:\n        return True\n    return any(\n        isinstance(n, ast.Assign) for n in ast.parse(expr, mode='exec').body\n    )","tryCatchPattern":"try:\n    df.eval(expr, inplace=True)\nexcept ValueError as e:\n    if 'no assignment' in str(e):\n        result = df.eval(expr, inplace=False)  # fall back to returning\n    else:\n        raise","preventionTips":["Only set inplace=True when the expression assigns a column.","Default to inplace=False and capture the return value.","Reserve inplace for 'col = ...' style expressions."],"tags":["pandas","eval","inplace","assignment"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}