pandas/mixed-offsets-without-utc
| Pack | Severity | Fix | Versions |
|---|---|---|---|
| pandas | warning | unsafe, with xpd check --fix --unsafe-fixes |
All versions |
On pandas 3, to_datetime raises ValueError when texts with a UTC offset hold more than one offset, as local times do across a daylight saving change. Pass utc=True, then convert with dt.tz_convert.
times = pd.to_datetime(df["time"], format="%Y-%m-%d %H:%M%z")times = pd.to_datetime(df["time"], format="%Y-%m-%d %H:%M%z", utc=True).dt.tz_convert("Europe/Copenhagen")What the rule reports
Section titled “What the rule reports”The rule reports pd.to_datetime(s, format="%Y-%m-%d %H:%M%z") without utc=, which raises ValueError on pandas 3.1 when the text holds more than one offset. Local times across a daylight saving change hold two offsets, such as +0100 and +0200.
Version, on text with offsets +0100 and +0200 |
Result |
|---|---|
| pandas 2.3 | an object Index and a FutureWarning |
| pandas 3.1 | ValueError |
How to fix it
Section titled “How to fix it”xpd check --fix --unsafe-fixes adds utc=True to pd.to_datetime(...) so pandas 3 parses mixed offsets without raising ValueError. It is unsafe because on a column with a single offset utc=True changes the resulting time zone to UTC, and you may still want .dt.tz_convert(...) for local wall times.
Diagnostic message
Section titled “Diagnostic message”This format reads a UTC offset, and on pandas 3 to_datetime raises ValueError when the offsets differ, as local times do across a daylight saving change. Pass utc=True, then convert with dt.tz_convert.See all rules in the pandas pack.