{"record":{"id":"07caeb5bc9c54605","repo":"pandas-dev/pandas","slug":"target-is-both-the-pipe-target-and-a-keyword-arg","errorCode":null,"errorMessage":"{target} is both the pipe target and a keyword argument","messagePattern":"(.+?) is both the pipe target and a keyword argument","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/common.py","lineNumber":536,"sourceCode":"        Function to apply to this object or, alternatively, a\n        ``(callable, data_keyword)`` tuple where ``data_keyword`` is a\n        string indicating the keyword of ``callable`` that expects the\n        object.\n    *args : iterable, optional\n        Positional arguments passed into ``func``.\n    **kwargs : dict, optional\n        A dictionary of keyword arguments passed into ``func``.\n\n    Returns\n    -------\n    object : the return type of ``func``.\n    \"\"\"\n    if isinstance(func, tuple):\n        # Assigning to func_ so pyright understands that it's a callable\n        func_, target = func\n        if target in kwargs:\n            msg = f\"{target} is both the pipe target and a keyword argument\"\n            raise ValueError(msg)\n        kwargs[target] = obj\n        return func_(*args, **kwargs)\n    else:\n        return func(obj, *args, **kwargs)\n\n\ndef get_rename_function(mapper: Any) -> Callable:\n    \"\"\"\n    Returns a function that will map names/labels, dependent if mapper\n    is a dict, Series or just a function.\n    \"\"\"\n\n    def f(x: Hashable) -> Any:\n        if x in mapper:\n            return mapper[x]\n        else:\n            return x\n","sourceCodeStart":518,"sourceCodeEnd":554,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/common.py#L518-L554","documentation":"Raised by pandas.core.common.pipe (pandas/core/common.py:536) when pipe is called with a (callable, target) tuple and the same `target` name is also supplied as a keyword argument. pipe injects the object into the callable via that keyword, so a duplicate keyword would be ambiguous; the conflict is detected and reported rather than silently overwritten.","triggerScenarios":"`df.pipe((func, 'df'), df=other)` where 'df' is both the tuple's target string and a kwarg. Also `df.pipe((plt.plot, 'x'), x=vals)` style calls with a clash.","commonSituations":"Generic pipe wrappers that forward **kwargs into a (callable, target) tuple call, where the caller also passes the target name. Refactors that rename the target but forget to update kwargs.","solutions":["Rename either the tuple target or the clashing keyword so they differ: `df.pipe((func, 'df'), data=other)`.","If the callable truly needs both the piped object and an explicit value under the same name, restructure the function signature to use distinct parameters.","Drop the tuple form and call directly: `func(df, other)` when no indirection is needed."],"exampleFix":"# before\ndf.pipe((func, 'df'), df=other)\n\n# after\ndf.pipe((func, 'df'), data=other)","handlingStrategy":"validation","validationCode":"def validate_pipe_target(func, kwargs):\n    if isinstance(func, tuple):\n        _, target = func\n        if target in kwargs:\n            raise ValueError(f\"'{target}' is both the pipe target and a keyword argument\")\n","typeGuard":"def has_pipe_target_conflict(func, kwargs) -> bool:\n    return isinstance(func, tuple) and func[1] in kwargs","tryCatchPattern":"try:\n    result = df.pipe((func, 'df'), **kwargs)\nexcept ValueError as e:\n    if 'both the pipe target' in str(e):\n        target = (func if isinstance(func, tuple) else (None, None))[1]\n        kwargs = {k: v for k, v in kwargs.items() if k != target}\n        kwargs[target + '_'] = df\n        result = df.pipe((func, target + '_'))\n    else:\n        raise","preventionTips":["Keep the tuple-target name distinct from any forwarded kwargs.","Avoid forwarding **kwargs into a (callable, target) pipe call without filtering.","Rename function parameters so the piped-object name does not collide."],"tags":["pipe","kwargs","argument-conflict","valueerror"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}