{"record":{"id":"aa9d169f39a562d3","repo":"pandas-dev/pandas","slug":"only-named-functions-are-supported","errorCode":null,"errorMessage":"Only named functions are supported","messagePattern":"Only named functions are supported","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/computation/expr.py","lineNumber":671,"sourceCode":"            else:\n                resolved = visited_value(self.env)\n            try:\n                v = getattr(resolved, attr)\n                name = self.env.add_tmp(v)\n                return self.term_type(name, self.env)\n            except AttributeError:\n                # something like datetime.datetime where scope is overridden\n                if isinstance(value, ast.Name) and value.id == attr:\n                    return resolved\n                raise\n\n        raise ValueError(f\"Invalid Attribute context {type(ctx).__name__}\")\n\n    def visit_Call(self, node, side=None, **kwargs):\n        if isinstance(node.func, ast.Attribute) and node.func.attr != \"__call__\":\n            res = self.visit_Attribute(node.func)\n        elif not isinstance(node.func, ast.Name):\n            raise TypeError(\"Only named functions are supported\")\n        else:\n            try:\n                res = self.visit(node.func)\n            except UndefinedVariableError:\n                # Check if this is a supported function name\n                try:\n                    res = FuncNode(node.func.id)\n                except ValueError:\n                    # Raise original error\n                    raise\n\n        if res is None:\n            # error: \"expr\" has no attribute \"id\"\n            raise ValueError(\n                f\"Invalid function call {node.func.id}\"  # type: ignore[union-attr]\n            )\n        if hasattr(res, \"value\"):\n            res = res.value","sourceCodeStart":653,"sourceCodeEnd":689,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/computation/expr.py#L653-L689","documentation":"visit_Call (expr.py:667) first handles ast.Attribute funcs (like obj.method()) and then requires the func to be an ast.Name. If node.func is anything else (a Lambda expression, a parenthesized expression, a subscript result being called), there is no callable name to resolve, so TypeError is raised. The eval grammar supports named functions and attribute calls only.","triggerScenarios":"df.eval('(lambda x: x + 1)(a)'), df.eval('(f or g)(a)'), or calling the result of any non-Name, non-Attribute expression.","commonSituations":"Trying to inline lambdas or higher-order calls in an eval string. Porting functional-style Python into eval.","solutions":["Define the function as a named callable and reference it by name (works only if registered in scope).","Compute the transformation in plain Python and assign the result back to a column.","For math, use the supported named functions (sin, cos, log, abs, sqrt, ...)."],"exampleFix":"// before\ndf.eval('(lambda x: x + 1)(a)')\n// after\ndf['a'] = df['a'].map(lambda x: x + 1)","handlingStrategy":"validation","validationCode":"import ast\n\ndef validate_call_target_is_name_or_attr(expr: str) -> None:\n    for node in ast.walk(ast.parse(expr, mode='eval')):\n        if isinstance(node, ast.Call):\n            if not isinstance(node.func, (ast.Name, ast.Attribute)):\n                raise TypeError(\n                    'only named functions or attribute calls are supported in eval'\n                )\n\nvalidate_call_target_is_name_or_attr(expr)","typeGuard":"import ast\n\ndef calls_are_named(expr: str) -> bool:\n    return all(\n        isinstance(n.func, (ast.Name, ast.Attribute))\n        for n in ast.walk(ast.parse(expr, mode='eval'))\n        if isinstance(n, ast.Call)\n    )","tryCatchPattern":"try:\n    df.eval(expr)\nexcept TypeError as e:\n    if 'Only named functions' in str(e):\n        # evaluate the transformation in Python instead\n        df['result'] = df['a'].map(some_fn)\n    raise","preventionTips":["Avoid lambdas and higher-order calls inside eval strings.","Register named functions in local_dict/global_dict if you need custom callables.","Prefer the supported math function names for numeric work."],"tags":["pandas","eval","functions","lambda","call"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}