{"record":{"id":"c3f3aff095ab0db6","repo":"pandas-dev/pandas","slug":"function-names-must-be-unique-if-there-is-no-new-c","errorCode":null,"errorMessage":"Function names must be unique if there is no new column names assigned","messagePattern":"Function names must be unique if there is no new column names assigned","errorType":"exception","errorClass":"SpecificationError","httpStatus":null,"severity":"error","filePath":"pandas/core/apply.py","lineNumber":372,"sourceCode":"            If the transform function fails or does not transform.\n        \"\"\"\n        obj = self.obj\n        func = self.func\n        axis = self.axis\n        args = self.args\n        kwargs = self.kwargs\n\n        is_series = obj.ndim == 1\n\n        if obj._get_axis_number(axis) == 1:\n            assert not is_series\n            return obj.T.transform(func, 0, *args, **kwargs).T\n\n        if is_list_like(func) and not is_dict_like(func):\n            func = cast(\"list[AggFuncTypeBase]\", func)\n            # GH#54929 - raise if duplicate function names are passed\n            if len(func) > len(set(func)):\n                raise SpecificationError(\n                    \"Function names must be unique if there is no new column names \"\n                    \"assigned\"\n                )\n            # Convert func equivalent dict\n            if is_series:\n                func = {com.get_callable_name(v) or v: v for v in func}\n            else:\n                func = dict.fromkeys(obj, func)\n\n        if is_dict_like(func):\n            func = cast(\"AggFuncTypeDict\", func)\n            return self.transform_dict_like(func)\n\n        # func is either str or callable\n        func = cast(\"AggFuncTypeBase\", func)\n        try:\n            result = self.transform_str_or_callable(func)\n        except TypeError:","sourceCodeStart":354,"sourceCodeEnd":390,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/apply.py#L354-L390","documentation":"When transform() is called with a list of functions (not a dict), pandas converts the list to a dict using function names as keys. If the list contains duplicate functions or multiple functions with the same name, the resulting dict would lose entries due to key collision. To prevent silent data loss (GH#54929), pandas raises a SpecificationError when len(func) > len(set(func)).","triggerScenarios":"Calling df.transform(['mean', 'mean']) — the same function string twice. Passing a list with two lambda functions (both named '<lambda>'). Passing a list with two functions that share the same __name__ attribute. Passing a list containing duplicate callable references.","commonSituations":"Programmatically building a function list that can include duplicates. Using multiple anonymous lambdas in a transform list (they all get the name '<lambda>'). Copy-pasting a function list and accidentally duplicating an entry.","solutions":["If using lambdas, assign them to named variables first: f1 = lambda x: ...; f2 = lambda x: ...; df.transform([f1, f2]).","Remove duplicates from the function list before passing: df.transform(list(dict.fromkeys(func_list))).","Use a dict to assign explicit output column names: df.transform({'out1': func1, 'out2': func2})."],"exampleFix":"# before\ndf.transform([lambda x: x.mean(), lambda x: x.sum()])\n# both lambdas have __name__ '<lambda>' -> collision\n\n# after — use named functions or a dict\nimport types\nf1 = types.FunctionType(lambda_func1.__code__, {}, 'mean_func')\n# or simply:\ndf.transform({'mean_col': lambda x: x.mean(), 'sum_col': lambda x: x.sum()})","handlingStrategy":"validation","validationCode":"def safe_transform(df_or_series, func_list, **kwargs):\n    if isinstance(func_list, list) and len(func_list) > len(set(func_list)):\n        raise ValueError(\n            \"Duplicate functions in transform list; use a dict to assign unique names\"\n        )\n    return df_or_series.transform(func_list, **kwargs)","typeGuard":"def has_unique_functions(func_list) -> bool:\n    if not isinstance(func_list, list):\n        return True\n    return len(func_list) == len(set(func_list))","tryCatchPattern":"try:\n    result = df.transform(func_list)\nexcept Exception as e:\n    if \"Function names must be unique\" in str(e):\n        # convert to dict with unique names\n        func_dict = {f\"func_{i}\": f for i, f in enumerate(func_list)}\n        result = df.transform(func_dict)\n    else:\n        raise","preventionTips":["Avoid passing duplicate functions in a transform list.","When using lambdas, assign them to named variables or wrap in a dict with explicit output names.","Use functools.partial or named helper functions instead of multiple bare lambdas."],"tags":["pandas","transform","duplicate-functions","named-aggregation","specificationerror"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}