pandas/column-loop
| Pack | Severity | Fix | Versions |
|---|---|---|---|
| pandas | warning | none, edit by hand | All versions |
A loop that reduces one column per pass makes a separate call for every column. Select the columns at once and reduce them in one call, which returns one value per column.
rows = []for name in models: error = (table[name] - table["published"]).dropna() rows.append((name, len(error), error.abs().mean(), error.mean()))error = table[models].sub(table["published"], axis=0)scores = pd.DataFrame({"n": error.count(), "mae": error.abs().mean(), "bias": error.mean()})What the rule reports
Section titled “What the rule reports”The rule reports a loop or a comprehension over column names that reduces one column per pass,
or that does arithmetic between that column and another column of the same frame. It also
reports a loop that builds the column name from the loop variable in an f-string, and a loop
that writes .shift, .diff, .pct_change, .clip, .rank, a cumulative method, .round,
.interpolate, .ffill or .bfill of one column per pass, as in
df[f"{col}_lag1"] = df[col].shift(1).
How to fix it
Section titled “How to fix it”xpd has no automatic fix, so make the change by hand.
A frame takes a list of names and reduces every column in one call, and the result is a table
with one row per column. On pandas 3.1.1 the two forms gave the same count, mean absolute error,
mean and root mean square, within 1e-16. For names built in an f-string, build the list of names
once, as in raw[[f"{ic} Volume" for ic in ZONE]].fillna(0.0).
For per-column transforms, transform the selected columns in one call and name the new ones with
add_suffix, as in df = df.join(df[cols].shift(1).add_suffix("_lag1")).
Diagnostic message
Section titled “Diagnostic message”This reduces one column of $FRAME per pass. Select the columns at once and reduce them in one call, as in $FRAME[names].sub($FRAME["base"], axis=0).abs().mean(), which returns one value per column.See all rules in the pandas pack.