{"record":{"id":"6b1b7947ba62f814","repo":"pola-rs/polars","slug":"todo-map-target-map-target-r","errorCode":null,"errorMessage":"TODO: map_target = {map_target!r}","messagePattern":"TODO: map_target = (.+?)","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/_utils/udfs.py","lineNumber":367,"sourceCode":"        return col_expr\n    elif map_target == \"series\":\n        if _RE_SERIES_NAMES.match(expression):\n            return expression.split(\".\", 1)[0]\n\n        # note: handle overlapping name from global variables; fallback\n        # through \"s\", \"srs\", \"series\" and (finally) srs0 -> srsN...\n        search_expr = expression.replace(col_expr, \"\")\n        for name in (\"s\", \"srs\", \"series\"):\n            if not re.search(rf\"\\b{name}\\b\", search_expr):\n                return name\n        n = count()\n        while True:\n            name = f\"srs{next(n)}\"\n            if not re.search(rf\"\\b{name}\\b\", search_expr):\n                return name\n\n    msg = f\"TODO: map_target = {map_target!r}\"\n    raise NotImplementedError(msg)\n\n\nclass BytecodeParser:\n    \"\"\"Introspect UDF bytecode and determine if we can rewrite as native expression.\"\"\"\n\n    _map_target_name: str | None = None\n    _can_attempt_rewrite: bool | None = None\n    _caller_variables: dict[str, Any] | None = None\n    _col_expression: tuple[str, str] | NoDefault | None = NO_DEFAULT\n\n    def __init__(self, function: Callable[[Any], Any], map_target: MapTarget) -> None:\n        \"\"\"\n        Initialize BytecodeParser instance and prepare to introspect UDFs.\n\n        Parameters\n        ----------\n        function : callable\n            The function/lambda to disassemble and introspect.","sourceCodeStart":349,"sourceCodeEnd":385,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/_utils/udfs.py#L349-L385","documentation":"polars's UDF bytecode parser in _utils/udfs.py tries to resolve the name of the object a `map`-style function was called on (the map target). If the target object type is one the parser does not recognize, it raises NotImplementedError with the repr of the target, marking an unmapped internal case. This is an explicit 'not yet supported' marker, not a user-input validation error.","triggerScenarios":"Calling map_elements / passing a Python function to map/apply whose map_target is not an Expr or Series (e.g. some lambda/callable or wrapper object the parser cannot classify); the parser's _get_target_name hits an unknown target kind while building a native expression rewrite.","commonSituations":"Using an exotic callable or functools.partial-like object as a UDF, or hitting a polars version where a new map-target kind was added without parser support; reporting an upstream bug after the message asks for it.","solutions":["Simplify the UDF: pass a plain function applied to an Expr/Series so polars can identify the map target.","Rewrite the mapping as native polars expressions (col arithmetic, when/then, replace_strict) to avoid bytecode parsing entirely.","Check your polars version and upgrade; if it persists, file an issue at pola-rs/polars with the function and the repr from the message."],"exampleFix":"// before\ndf.select(pl.col('a').map_elements(lambda x: x*2))\n// after\ndf.with_columns((pl.col('a') * 2).alias('a'))","handlingStrategy":"validation","validationCode":"def is_supported_map_target(fn):\n    return callable(fn) and not isinstance(fn, type)  # keep targets simple; prefer Expr/Series-facing APIs","typeGuard":"def is_udf_safe(fn) -> bool:\n    import inspect\n    return inspect.isfunction(fn) and not inspect.iscoroutinefunction(fn)","tryCatchPattern":"try:\n    df.select(expr.map_elements(fn))\nexcept NotImplementedError as e:\n    if 'TODO: map_target' in str(e):\n        # fall back to plain Python or native expression\n        ...","preventionTips":["Prefer native polars expressions over Python UDFs","Keep UDFs as plain functions, not partials/wrappers","Upgrade polars before assuming a bug is yours"],"tags":["python","polars","udf","not-implemented"],"backgroundTag":"unsupported-operation","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}