Skip to content

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()

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.

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.

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.