pandas/apply-math-function
| Pack | Severity | Fix | Versions |
|---|---|---|---|
| pandas | warning | unsafe, with xpd check --fix --unsafe-fixes |
All versions |
Passing a math function such as np.sqrt to apply or map calls it once for every value. Call the function on the whole column.
root = df["wind"].map(np.sqrt)root = np.sqrt(df["wind"])What the rule reports
Section titled “What the rule reports”The rule reports df["w"].map(np.sqrt), df["w"].apply(lambda v: np.log(v)), df["w"].apply(math.sqrt) and df["b"].apply(abs), which call the function once per value. With a bare function name it reports apply only on a column taken by a text label, because on a frame apply works per column. It leaves df["w"].apply(np.sqrt) alone, because Series.apply already hands a NumPy function the whole column. map calls the function once per value.
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 np.sqrt(df["w"]), df["b"].abs().
The fix is unsafe, because math.sqrt and math.log raise on a value outside their domain, where NumPy returns NaN.
A column of Python objects can handle abs differently in the original call and the column method. The rewrite can also change the dtype.
Diagnostic message
Section titled “Diagnostic message”This calls $F once per value. Write $F($S) on the whole column.See all rules in the pandas pack.