pandas/apply-on-dates
| Rule | apply-on-dates |
|---|---|
| Pack | pandas |
| Severity | warning |
| Fix | unsafe, with xpd check --fix --unsafe-fixes |
| Library versions | every version |
Date arithmetic inside apply runs Python once for every date. Add or subtract on the whole column.
next_week = df["time"].apply(lambda time: time + pd.Timedelta(days=7))next_week = df["time"] + pd.Timedelta(days=7)What the rule reports
Section titled “What the rule reports”The rule reports df["d"].apply(lambda t: t + pd.Timedelta(days=7)), t.replace(day=1) and t.strftime(...), which call Python once per date. For replace(day=1) the fix writes df["d"] - pd.to_timedelta(df["d"].dt.day - 1, unit="D"), which keeps the time of day.
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["d"] + pd.Timedelta(days=7).
The fixes are unsafe, because a missing date gives a missing result where the lambda raised.
Message
Section titled “Message”xpd prints this message with the issue. A name that starts with $ stands for code from your file.
Date arithmetic per value runs in Python. Write $S + $D on the whole column.Browse the other rules of the pandas pack.