{"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/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/apply.py#L228-L264","documentation":"Named aggregation (e.g., df.agg(result=('col', 'func'))) is a syntax that assigns output column names. It is only supported when aggregating along axis=0 (the default, operating on columns). When axis=1 is specified (row-wise operation), named aggregation is not implemented and raises NotImplementedError. The f-string uses {axis=} which produces 'axis=1' in the message.","triggerScenarios":"Calling df.agg(new_name=('col', 'mean'), axis=1) — using named aggregation kwargs with axis=1. Calling df.apply(...) or df.agg(...) with a dict-like func that includes column assignments and axis=1 simultaneously.","commonSituations":"Trying to apply column-name-preserving aggregation row-wise. Adapting an axis=0 named-aggregation call to axis=1 by just changing the axis parameter. Confusing the semantics of axis=1 (row-wise apply) with named column output.","solutions":["Remove named aggregation when using axis=1: use a plain function or list of functions instead.","If you need named output with row-wise operations, apply first, then rename: df.apply(func, axis=1).rename(columns=...).","Use axis=0 (default) if named aggregation is important to your workflow."],"exampleFix":"# before\ndf.agg(result=('col', 'mean'), axis=1)\n\n# after — use a plain function for row-wise operations\ndf.apply(lambda row: row['col'].mean(), axis=1)","handlingStrategy":"validation","validationCode":"def safe_agg(df, axis=0, **kwargs):\n    # Check for named aggregation kwargs with axis=1\n    if axis == 1 and any(isinstance(v, tuple) and len(v) == 2 for v in kwargs.values()):\n        raise NotImplementedError(\n            \"Named aggregation is not supported with axis=1; use axis=0 or plain functions\"\n        )\n    return df.agg(axis=axis, **kwargs)","typeGuard":"def uses_named_aggregation(kwargs) -> bool:\n    return any(isinstance(v, tuple) for v in kwargs.values())","tryCatchPattern":"try:\n    result = df.agg(new_name=('col', 'mean'), axis=1)\nexcept NotImplementedError as e:\n    if \"Named aggregation\" in str(e):\n        result = df.apply(lambda row: row['col'].mean(), axis=1)\n    else:\n        raise","preventionTips":["Never combine named aggregation kwargs with axis=1.","Use axis=0 (default) for named aggregation, or use plain functions with axis=1.","Remember the named aggregation syntax: df.agg(output=(input_col, func))."],"tags":["pandas","agg","named-aggregation","axis","notimplementederror"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}