pandas/to-datetime-then-localize
| Rule | to-datetime-then-localize |
|---|---|
| Pack | pandas |
| Severity | warning |
| Fix | unsafe, with xpd check --fix --unsafe-fixes |
| Library versions | every version |
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.
Message
Section titled “Message”xpd prints this message with the issue. A name that starts with $ stands for code from your file.
to_datetime takes utc=True, which also reads text that carries an offset, where tz_localize raises. Write $PD.to_datetime($$$A, utc=True).Browse the other rules of the pandas pack.