pandas/script-steps
| Rule | script-steps |
|---|---|
| Pack | pandas |
| Severity | info, and warning with style = "strict". A rule of the house style |
| Fix | none, edit by hand |
| Library versions | every version |
A module that builds its frames in ten or more statements at its top, with no function that takes a frame, runs only from its first line to its last. Move the statements into step functions and join them with pipe.
df = pd.read_parquet("orders.parquet")df = df[df["price"] > 0]df["total"] = df["price"] * df["qty"]df = df.merge(cities, on="city", validate="many_to_one")def keep_sold(frame: pd.DataFrame) -> pd.DataFrame: return frame.loc[pd.col("price") > 0]
def add_total(frame: pd.DataFrame) -> pd.DataFrame: return frame.assign(total=pd.col("price") * pd.col("qty"))
def add_city(frame: pd.DataFrame, cities: pd.DataFrame) -> pd.DataFrame: return frame.merge(cities, on="city", validate="many_to_one")
df = ( pd.read_parquet("orders.parquet") .pipe(keep_sold) .pipe(add_total) .pipe(add_city, cities))What the rule reports
Section titled “What the rule reports”The rule reports the first statement of a module that has both of the following:
- ten or more statements at its top that give a name a frame, as in
df = df.dropna(), or write into a frame, as indf["x"] = ... - no function that takes a frame
xpd counts a statement only when it can prove the frame. The value must be a call such as pd.read_parquet(...), a method of a frame that returns a frame, or a selection of rows from a frame. A statement inside a function, a loop or a branch does not count. The rule leaves alone a file with a cell marker (# %%, # <codecell> or # In[1]:), because an editor runs such a file cell by cell. A function takes a frame when a parameter is annotated pd.DataFrame, when a call passes it a frame, or when a .pipe(...) call names it.
The script and its rewrite as step functions gave an equal frame, by xpd equiv. xpd graph reads the pipe form as a pipeline of steps, and it lists the columns that each step reads and adds.
How to fix it
Section titled “How to fix it”xpd has no automatic fix, so make the change by hand.
Message
Section titled “Message”xpd prints this message with the issue. A name that starts with $ stands for code from your file.
This module builds its frames in ten or more statements at its top, and no function of it takes a frame, so no step can be called or checked alone. Move the statements into functions that take a frame and return one, and join them with pipe, as in pd.read_parquet(path).pipe(clean).pipe(add_columns). xpd graph reads that form as a pipeline of steps.Browse the other rules of the pandas pack.