pandas-dev/pandas · error · TypeError

Only named functions are supported

Error message

Only named functions are supported

What it means

Raised by visit_Call() when node.func is neither an ast.Attribute (method call) nor an ast.Name (bare function name). pandas eval supports calling named functions (e.g., 'sin(x)') and method calls on resolved objects (e.g., 'obj.method(x)'), but it does not support calling the result of an arbitrary sub-expression (e.g., '(lambda x: x)(5)' or calling a subscripted item like 'fns[0](x)'). The function being called must be a direct name or attribute.

Solutions

  1. Call only named functions: df.eval('sin(col)') where 'sin' is a supported math function, or reference a named function in scope.
  2. Move dynamic/lambda calls out of the eval string into regular Python.
  3. For method calls, use attribute syntax: df.eval('col.sum()') is not how eval works — use df['col'].sum() directly.

Example fix

// before
df.eval('(lambda x: x + 1)(col)')

// after
df['col'] = df['col'].map(lambda x: x + 1)
Defensive patterns

Strategy: validation

Validate before calling

import ast
def calls_use_named_funcs(expr: str) -> bool:
    try:
        tree = ast.parse(expr, mode='eval')
    except SyntaxError:
        return True
    for node in ast.walk(tree):
        if isinstance(node, ast.Call) and not isinstance(node.func, (ast.Name, ast.Attribute)):
            return False
    return True
if not calls_use_named_funcs(expr):
    raise ValueError('Only named functions or methods are callable in eval')
df.eval(expr)

Try / catch

try:
    df.eval(expr)
except TypeError as e:
    if 'named functions' in str(e):
        # move dynamic calls to regular Python
        ...

Prevention

When it happens

Trigger: df.eval('(lambda x: x)(5)') — the callable is a lambda expression, not a Name/Attribute; df.eval('fns[0](x)') — callable is a subscript; any expression where the call target is itself a complex expression.

Common situations: Trying to call dynamically-selected functions or inline lambdas inside the eval string; expecting first-class function semantics from the restricted eval DSL.

Related errors


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

Appendix: source

Thrown at pandas/core/computation/expr.py:671

            else:
                resolved = visited_value(self.env)
            try:
                v = getattr(resolved, attr)
                name = self.env.add_tmp(v)
                return self.term_type(name, self.env)
            except AttributeError:
                # something like datetime.datetime where scope is overridden
                if isinstance(value, ast.Name) and value.id == attr:
                    return resolved
                raise

        raise ValueError(f"Invalid Attribute context {type(ctx).__name__}")

    def visit_Call(self, node, side=None, **kwargs):
        if isinstance(node.func, ast.Attribute) and node.func.attr != "__call__":
            res = self.visit_Attribute(node.func)
        elif not isinstance(node.func, ast.Name):
            raise TypeError("Only named functions are supported")
        else:
            try:
                res = self.visit(node.func)
            except UndefinedVariableError:
                # Check if this is a supported function name
                try:
                    res = FuncNode(node.func.id)
                except ValueError:
                    # Raise original error
                    raise

        if res is None:
            # error: "expr" has no attribute "id"
            raise ValueError(
                f"Invalid function call {node.func.id}"  # type: ignore[union-attr]
            )
        if hasattr(res, "value"):
            res = res.value

View on GitHub (pinned to 3b7651241d)