{"record":{"id":"2779ed1e6d3d5aaa","repo":"pandas-dev/pandas","slug":"named-aggregation-is-not-supported-when-axis","errorCode":null,"errorMessage":"Named aggregation is not supported when {axis=}.","messagePattern":"Named aggregation is not supported when (.+?)\\.","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"pandas/core/apply.py","lineNumber":246,"sourceCode":"    axis: Axis = 0,\n    raw: bool = False,\n    result_type: str | None = None,\n    by_row: Literal[False, \"compat\"] = \"compat\",\n    engine: str = \"python\",\n    engine_kwargs: dict[str, bool] | None = None,\n    args=None,\n    kwargs=None,\n) -> FrameApply:\n    \"\"\"construct and return a row or column based frame apply object\"\"\"\n    _, func, columns, _ = reconstruct_func(func, **kwargs)\n\n    axis = obj._get_axis_number(axis)\n    klass: type[FrameApply]\n    if axis == 0:\n        klass = FrameRowApply\n    elif axis == 1:\n        if columns:\n            raise NotImplementedError(\n                f\"Named aggregation is not supported when {axis=}.\"\n            )\n        klass = FrameColumnApply\n\n    return klass(\n        obj,\n        func,\n        raw=raw,\n        result_type=result_type,\n        by_row=by_row,\n        engine=engine,\n        engine_kwargs=engine_kwargs,\n        args=args,\n        kwargs=kwargs,\n    )\n\n\nclass Apply(metaclass=abc.ABCMeta):","sourceCodeStart":228,"sourceCodeEnd":264,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/apply.py#L228-L264","documentation":"Raised by frame_apply when named aggregation (the **kwargs-as-named-columns syntax like df.agg(col=('sum')) or the dict/tuple form that resolves to columns) is used together with axis=1. Named aggregation defines output column names by applying functions to columns, which is only meaningful across rows (axis=0); applying it across columns is not implemented.","triggerScenarios":"df.agg(total=('A', 'sum'), axis=1); df.apply({'x': 'sum'}, axis=1) where the func resolves to named-aggregation columns; df.transform with named kwargs and axis=1.","commonSituations":"Switching an existing named-aggregation call from axis=0 to axis=1 expecting row-wise named output; generic apply wrappers that forward both named kwargs and an axis parameter.","solutions":["Use axis=0 (the default) for named aggregation.","For row-wise operations, use df.apply(func, axis=1) with a plain function (no named-aggregation kwargs) and rename the resulting Series afterward.","Drop the named-aggregation kwargs when you must operate across columns."],"exampleFix":"# before\ndf.agg(total=('A', 'sum'), axis=1)\n# after\ndf.agg(total=('A', 'sum'))  # axis=0\ndf.apply(lambda row: row['A'].sum(), axis=1).rename('total')","handlingStrategy":"validation","validationCode":"def named_agg(df, axis=0, **kwargs):\n    has_named = any(isinstance(v, tuple) for v in kwargs.values())\n    if has_named and axis == 1:\n        raise NotImplementedError('named aggregation requires axis=0')\n    return df.agg(axis=axis, **kwargs)","typeGuard":"def is_named_aggregation(func) -> bool:\n    if isinstance(func, dict):\n        return any(isinstance(v, tuple) and len(v) == 2 for v in func.values())\n    return False","tryCatchPattern":null,"preventionTips":["Keep named aggregation on axis=0 (the default).","For row-wise output use df.apply(func, axis=1) and rename the result.","Avoid forwarding both named kwargs and a user axis into agg/apply."],"tags":["apply","named-aggregation","axis","dataframe"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}