pandas/to-datetime-then-localize
| Pack | Severity | Fix | Versions |
|---|---|---|---|
| pandas | warning | unsafe, with xpd check --fix --unsafe-fixes |
All versions |
tz_localize("UTC") after to_datetime raises on text that carries an offset. Pass utc=True to to_datetime instead.
when = pd.to_datetime(df["raw"]).dt.tz_localize("UTC")when = pd.to_datetime(df["raw"], utc=True)What the rule reports
Section titled “What the rule reports”The rule reports pd.to_datetime(df["raw"]).dt.tz_localize("UTC"), where utc=True sets the zone in the same call.
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 pd.to_datetime(df["raw"], utc=True).
The fix is unsafe, because on text that holds an offset utc=True converts to UTC where tz_localize raised TypeError. The fix adds an argument after the last one, so the rule leaves a call alone that ends in a trailing comma or holds a comment.
Diagnostic message
Section titled “Diagnostic message”to_datetime takes utc=True, which also reads text that carries an offset, where tz_localize raises. Write $PD.to_datetime($$$A, utc=True).See all rules in the pandas pack.