Skip to content

pandas/strftime-round-trip

Pack Severity Fix Versions
pandas warning unsafe, with xpd check --fix --unsafe-fixes All versions

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.

This formats every date as text and parses the text again to drop the time. Call $S.dt.normalize().

See all rules in the pandas pack.