{"record":{"id":"00e8e0c97a1ac75a","repo":"pandas-dev/pandas","slug":"multi-line-expressions-are-only-valid-in-the-conte","errorCode":null,"errorMessage":"multi-line expressions are only valid in the context of data, use DataFrame.eval","messagePattern":"multi-line expressions are only valid in the context of data, use DataFrame\\.eval","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/computation/eval.py","lineNumber":349,"sourceCode":"    1    pig   20          40\n    \"\"\"\n    inplace = validate_bool_kwarg(inplace, \"inplace\")\n\n    exprs: list[str | BinOp]\n    if isinstance(expr, str):\n        _check_expression(expr)\n        exprs = [e.strip() for e in expr.splitlines() if e.strip() != \"\"]\n    elif isinstance(expr, NDFrame):\n        # GH#16289 a Series/DataFrame would otherwise be converted to its\n        #  (possibly truncated) repr and parsed, producing a confusing error\n        raise ValueError(f\"expr must be a string to be evaluated, {type(expr)} given\")\n    else:\n        # ops.BinOp; for internal compat, not intended to be passed by users\n        exprs = [expr]\n    multi_line = len(exprs) > 1\n\n    if multi_line and target is None:\n        raise ValueError(\n            \"multi-line expressions are only valid in the \"\n            \"context of data, use DataFrame.eval\"\n        )\n    engine = _check_engine(engine)\n    _check_parser(parser)\n    _check_resolvers(resolvers)\n\n    ret = None\n    first_expr = True\n    target_modified = False\n\n    for expr in exprs:\n        expr = _convert_expression(expr)\n        _check_for_locals(expr, level, parser)\n\n        # get our (possibly passed-in) scope\n        env = ensure_scope(\n            level + 1,","sourceCodeStart":331,"sourceCodeEnd":367,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/computation/eval.py#L331-L367","documentation":"Raised in the public eval() function when the expression string contains multiple lines (splitlines yields more than one non-empty line) but no target object is supplied. Multi-line expressions are only meaningful when there is a target (e.g., a DataFrame) to assign results into, because each line typically assigns a new column. Without a target, the multi-line semantics (which expect assignment) are undefined at top level.","triggerScenarios":"Calling pd.eval('a = 1\\nb = 2') with target=None (the default). Also triggered by expressions with embedded newlines from triple-quoted strings or from files.","commonSituations":"Using a multi-line expression string built for DataFrame.eval but calling pd.eval by mistake; building a list of expressions joined with newlines and forgetting to pass target=df.","solutions":["Use DataFrame.eval for multi-line expressions: df.eval('a = 1\\nb = 2') — the DataFrame is the target.","If using pd.eval, pass target=<your DataFrame or dict-like object>.","Split the expression into separate single-line pd.eval calls if you genuinely have no target."],"exampleFix":"// before\npd.eval('new = col_a + col_b\\nother = new * 2')\n\n// after\ndf.eval('new = col_a + col_b\\nother = new * 2')","handlingStrategy":"validation","validationCode":"is_multiline = isinstance(expr, str) and len([l for l in expr.splitlines() if l.strip()]) > 1\nif is_multiline and target is None:\n    raise ValueError('Multi-line expressions require a target — use df.eval')\npd.eval(expr, target=target)","typeGuard":null,"tryCatchPattern":"try:\n    pd.eval(expr, target=target)\nexcept ValueError as e:\n    if 'multi-line' in str(e).lower():\n        result = df.eval(expr)\n    else:\n        raise","preventionTips":["Use df.eval for multi-line expressions.","Pass target=df when using pd.eval with assignments.","Detect newlines in expr before calling pd.eval."],"tags":["pandas","eval","multiline","target","argument-error"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}