pandas/apply-text-method
| Pack | Severity | Fix | Versions |
|---|---|---|---|
| pandas | warning | unsafe, with xpd check --fix --unsafe-fixes |
All versions |
Calling a text method inside apply or map runs Python once for every value. Call the same method on the whole column through .str.
names = df["name"].map(lambda name: name.upper())names = df["name"].str.upper()What the rule reports
Section titled “What the rule reports”The rule reports df["s"].map(lambda v: v.startswith("A")), which calls Python once per value where the str accessor works on the whole column. The fix is unsafe, because a column method returns a missing value where the lambda raised. The dtype can also change, as when dt.year gives int32 where the lambda gave int64.
The rule leaves split alone because the lambda was faster. It also leaves keyword arguments alone, as in replace(day=1) on dates, and arguments that use the lambda parameter.
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["s"].str.startswith("A").
Diagnostic message
Section titled “Diagnostic message”Use $S.str.$M(...) instead of a Python function per value.See all rules in the pandas pack.