pandas-dev/pandas · error · SpecificationError
Function names must be unique if there is no new column…
Error message
Function names must be unique if there is no new column names assigned
What it means
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)).
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}).
Example fix
# before
df.transform([lambda x: x.mean(), lambda x: x.sum()])
# both lambdas have __name__ '<lambda>' -> collision
# after — use named functions or a dict
import types
f1 = types.FunctionType(lambda_func1.__code__, {}, 'mean_func')
# or simply:
df.transform({'mean_col': lambda x: x.mean(), 'sum_col': lambda x: x.sum()}) Defensive patterns
Strategy: validation
Validate before calling
def safe_transform(df_or_series, func_list, **kwargs):
if isinstance(func_list, list) and len(func_list) > len(set(func_list)):
raise ValueError(
"Duplicate functions in transform list; use a dict to assign unique names"
)
return df_or_series.transform(func_list, **kwargs) Type guard
def has_unique_functions(func_list) -> bool:
if not isinstance(func_list, list):
return True
return len(func_list) == len(set(func_list)) Try / catch
try:
result = df.transform(func_list)
except Exception as e:
if "Function names must be unique" in str(e):
# convert to dict with unique names
func_dict = {f"func_{i}": f for i, f in enumerate(func_list)}
result = df.transform(func_dict)
else:
raise Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- cannot combine transform and aggregation operations
- Function did not transform
- Named aggregation is not supported when
- nested renamer is not supported
- No transform functions were provided
AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11).
Data as JSON: /api/errors/c3f3aff095ab0db6.
Report an issue: GitHub.
Appendix: source
Thrown at pandas/core/apply.py:372
If the transform function fails or does not transform.
"""
obj = self.obj
func = self.func
axis = self.axis
args = self.args
kwargs = self.kwargs
is_series = obj.ndim == 1
if obj._get_axis_number(axis) == 1:
assert not is_series
return obj.T.transform(func, 0, *args, **kwargs).T
if is_list_like(func) and not is_dict_like(func):
func = cast("list[AggFuncTypeBase]", func)
# GH#54929 - raise if duplicate function names are passed
if len(func) > len(set(func)):
raise SpecificationError(
"Function names must be unique if there is no new column names "
"assigned"
)
# Convert func equivalent dict
if is_series:
func = {com.get_callable_name(v) or v: v for v in func}
else:
func = dict.fromkeys(obj, func)
if is_dict_like(func):
func = cast("AggFuncTypeDict", func)
return self.transform_dict_like(func)
# func is either str or callable
func = cast("AggFuncTypeBase", func)
try:
result = self.transform_str_or_callable(func)
except TypeError:View on GitHub (pinned to 3b7651241d)