pandas/nested-step-calls
| Rule | nested-step-calls |
|---|---|
| Pack | pandas |
| Severity | info, and warning with style = "strict". A rule of the house style |
| Fix | unsafe, with xpd check --fix --unsafe-fixes |
| Library versions | every version |
Three functions called one inside the other read from the inside out, so the first step is written last. Chain them with pipe, which lists the steps in the order they run.
df = pd.read_csv("sales.csv")result = add_shares(add_totals(clean(df)))result = df.pipe(clean).pipe(add_totals).pipe(add_shares)What the rule reports
Section titled “What the rule reports”The rule reports three functions called one inside the other on a frame, as in add_shares(add_totals(clean(df))). DataFrame.pipe(f) calls f(frame), and the two forms gave equal frames. The three functions must be plain names and not Python builtins. xpd must also know that the innermost argument is a frame, which means that the argument is one of the following:
- a call such as
pd.read_csv(...)orpd.DataFrame(...) - a variable whose nearest earlier assignment is such a call
- a parameter annotated
pd.DataFrame
The fix is unsafe, because the chain raises AttributeError when the first or the second function returns something without pipe, such as a dict. xpd graph reads the pipe form as a pipeline of steps.
How to fix it
Section titled “How to fix it”xpd check --fix --unsafe-fixes applies the fixes of this rule. Check the result, because an unsafe fix can change what the code does.
The unsafe fix writes df.pipe(clean).pipe(add_totals).pipe(add_shares).
Message
Section titled “Message”xpd prints this message with the issue. A name that starts with $ stands for code from your file.
These calls read from the inside out, so $A runs first and is written last. Write $X.pipe($A).pipe($B).pipe($C), which lists the steps in the order they run and which xpd graph reads as a pipeline.Browse the other rules of the pandas pack.