pandas-dev/pandas · error · ValueError

{target} is both the pipe target and a keyword argument

Error message

{target} is both the pipe target and a keyword argument

What it means

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.

Source

Thrown at pandas/core/common.py:536

        Function to apply to this object or, alternatively, a
        ``(callable, data_keyword)`` tuple where ``data_keyword`` is a
        string indicating the keyword of ``callable`` that expects the
        object.
    *args : iterable, optional
        Positional arguments passed into ``func``.
    **kwargs : dict, optional
        A dictionary of keyword arguments passed into ``func``.

    Returns
    -------
    object : the return type of ``func``.
    """
    if isinstance(func, tuple):
        # Assigning to func_ so pyright understands that it's a callable
        func_, target = func
        if target in kwargs:
            msg = f"{target} is both the pipe target and a keyword argument"
            raise ValueError(msg)
        kwargs[target] = obj
        return func_(*args, **kwargs)
    else:
        return func(obj, *args, **kwargs)


def get_rename_function(mapper: Any) -> Callable:
    """
    Returns a function that will map names/labels, dependent if mapper
    is a dict, Series or just a function.
    """

    def f(x: Hashable) -> Any:
        if x in mapper:
            return mapper[x]
        else:
            return x

View on GitHub (pinned to 71959b8cb9)

Solutions

  1. Rename either the tuple target or the clashing keyword so they differ: `df.pipe((func, 'df'), data=other)`.
  2. 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.
  3. Drop the tuple form and call directly: `func(df, other)` when no indirection is needed.

Example fix

# before
df.pipe((func, 'df'), df=other)

# after
df.pipe((func, 'df'), data=other)
Defensive patterns

Strategy: validation

Validate before calling

def validate_pipe_target(func, kwargs):
    if isinstance(func, tuple):
        _, target = func
        if target in kwargs:
            raise ValueError(f"'{target}' is both the pipe target and a keyword argument")

Type guard

def has_pipe_target_conflict(func, kwargs) -> bool:
    return isinstance(func, tuple) and func[1] in kwargs

Try / catch

try:
    result = df.pipe((func, 'df'), **kwargs)
except ValueError as e:
    if 'both the pipe target' in str(e):
        target = (func if isinstance(func, tuple) else (None, None))[1]
        kwargs = {k: v for k, v in kwargs.items() if k != target}
        kwargs[target + '_'] = df
        result = df.pipe((func, target + '_'))
    else:
        raise

Prevention

When it happens

Trigger: `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.

Common situations: 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.

Related errors


AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07). Data as JSON: /api/errors/07caeb5bc9c54605. Report an issue: GitHub.