Sub topic 10
Extra arguments and the (callable, keyword) form
This chapter assumes you can write a Python function with default and keyword parameters, and that you have used DataFrame.pipe to put a function into a chain. The goal here is to pass extra arguments through pipe, and to handle functions that do not take the DataFrame as their first parameter.
How pipe passes arguments
df.pipe(func, *args, **kwargs) calls func(df, *args, **kwargs). The DataFrame always goes first. Everything else you give to pipe follows it unchanged.
This lets you write one function and reuse it with different settings:
import pandas as pd
df = pd.DataFrame({
"order_id": [1, 2, 3, 4],
"region": ["east", "west", "east", "west"],
"price": [10.0, 20.0, 15.0, 5.0],
"quantity": [2, 1, 3, 10],
})
def add_tax(data, rate):
return data.assign(total=data["price"] * data["quantity"] * (1 + rate))
result = (
df
.pipe(add_tax, rate=0.2)
.query("total > 20")
)
print(result)The call .pipe(add_tax, rate=0.2) runs add_tax(df, rate=0.2). The function returns a new DataFrame with a total column, and query then filters it.
You can also pass extra arguments by position. .pipe(add_tax, 0.2) works the same way. Keyword arguments are easier to read in a long chain, because the name tells the reader what 0.2 means.
When the DataFrame is not the first parameter
Some functions are written with the data in another position. You may not control them, such as a function from a library or a teammate's module. Here is one:
def scale_price(factor, data):
return data.assign(price=data["price"] * factor)Calling df.pipe(scale_price, factor=2) fails. pipe puts df in the first slot, which is factor. Then it sees factor=2 as well. Python raises a TypeError saying the function got multiple values for factor.
The fix is the (callable, keyword) form. Replace the function with a two-item tuple. The first item is the function. The second is the name of the parameter that should receive the DataFrame.
result = df.pipe((scale_price, "data"), factor=2)
print(result)pipe now calls scale_price(data=df, factor=2). The extra arguments are passed as usual.
Rules for the tuple form
- The second item must be a string naming a parameter of the function.
- Do not also pass a keyword argument with that same name.
piperaises an error because the DataFrame would be supplied twice. - The tuple form works anywhere in a chain. It does not change what the function returns.
An alternative: a lambda wrapper
You can get the same result with a small lambda:
result = df.pipe(lambda d: scale_price(2, d))This works, but it hides the arguments inside the lambda body. The tuple form keeps the call in the standard pipe(func, **kwargs) shape. Use a lambda when you need to compute an argument from the data itself. Use the tuple when you only need to name the receiving parameter.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| `TypeError: got multiple values for argument` | The DataFrame landed in a parameter that you also set by keyword | Use `(func, "param_name")` |
| `TypeError: missing 1 required positional argument` | You forgot an extra argument | Add it to the `pipe` call |
| `KeyError` on a column | An earlier step renamed or dropped it | Print the columns before the failing step |
Exercise
A helper is defined as def keep_min(min_total, frame): return frame.query("price * quantity >= @min_total"). Write the pipe call that applies it to df with a threshold of 20, and explain why df.pipe(keep_min, min_total=20) fails.
Show answer
Use df.pipe((keep_min, "frame"), min_total=20). The plain call fails because pipe passes df as the first positional argument, which is min_total. Then min_total=20 supplies that parameter a second time, so Python raises a TypeError about multiple values. The tuple form tells pipe to send the DataFrame to the frame parameter instead.