pandas/strftime-round-trip
| Rule | strftime-round-trip |
|---|---|
| Pack | pandas |
| Severity | warning |
| Fix | unsafe, with xpd check --fix --unsafe-fixes |
| Library versions | every version |
Formatting dates as text and parsing them again to drop the time was 18 to 202 times slower. Call dt.normalize() instead.
day = pd.to_datetime(df["time"].dt.strftime("%Y-%m-%d"))day = df["time"].dt.normalize()What the rule reports
Section titled “What the rule reports”The rule reports pd.to_datetime(df["d"].dt.strftime("%Y-%m-%d")) and df["d"].dt.strftime("%Y-%m-%d") == "2023-06-01", which turn dates into text to drop the time and were 18 to 202 times slower than normalize.
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"].dt.normalize(), df["d"].dt.floor("h"), and df["d"].dt.normalize() == pd.Timestamp(day) for the comparison, which raises TypeError on a column with a time zone.
The fix is unsafe, because on dates with a time zone the round trip through text drops the zone.
Message
Section titled “Message”xpd prints this message with the issue. A name that starts with $ stands for code from your file.
This formats every date as text and parses the text again to drop the time. Call $S.dt.normalize().Browse the other rules of the pandas pack.