Sub topic 6
Parsing strings with UTC offsets
This chapter assumes you know the difference between naive and aware timestamps, and that you have used tz_localize and tz_convert. Here you will turn text such as 2024-06-01T00:00:00+02:00 into a clean tz-aware column.
What an offset tells you
A UTC offset such as +02:00 states how far a local clock is from UTC at that instant. It is not a time zone. It does not say which region the timestamp came from, and it carries no daylight saving rules. Berlin uses +01:00 in winter and +02:00 in summer, so one zone produces two different offsets.
Because an offset fixes the exact instant, pandas can convert any offset string to UTC without guessing. That makes UTC a safe common ground for parsing.
Parsing strings with one offset
If every string has the same offset, pd.to_datetime returns a tz-aware result that uses a fixed-offset zone.
import pandas as pd
same = pd.to_datetime(["2024-01-01T00:00:00+01:00", "2024-01-02T00:00:00+01:00"])
print(same.dtype) # datetime64[ns, UTC+01:00]This dtype is aware, but it is a constant shift. It will never change its offset in summer, so it is not the same as Europe/Berlin.
Parsing mixed offsets with utc=True
Real data often mixes offsets, for example when a log combines winter and summer entries from one region. Without help, pandas cannot put them in one datetime64 dtype. Older versions return an object-dtype result holding Python datetimes. Newer versions emit a FutureWarning, and the newest versions raise an error. Pass utc=True to avoid all of these outcomes.
import pandas as pd
raw = ["2024-01-01T00:00:00+01:00", "2024-06-01T00:00:00+02:00"]
parsed = pd.to_datetime(raw, utc=True)
print(parsed) # 2023-12-31 23:00:00+00:00 and 2024-05-31 22:00:00+00:00Each string is shifted by its own offset, then stored as UTC. The result is one tz-aware DatetimeIndex. Passing a Series gives a Series with a UTC dtype instead.
Getting back to local time
UTC is good for storage. For display or daily grouping you usually want a named zone. Convert the parsed result with tz_convert.
import pandas as pd
raw = ["2024-01-01T00:00:00+01:00", "2024-06-01T00:00:00+02:00"]
parsed = pd.to_datetime(raw, utc=True)
berlin = parsed.tz_convert("Europe/Berlin")
print(berlin) # 2024-01-01 00:00:00+01:00 and 2024-06-01 00:00:00+02:00The wall-clock times match the input, and the offsets now come from the Europe/Berlin rules. This only works because the strings were truly Berlin times. If the source system wrote offsets for another region, converting to Berlin gives different clock times.
Strings without an offset
With utc=True, strings that have no offset are treated as already being in UTC. That can silently be wrong if those strings are local times.
import pandas as pd
naive_strings = ["2024-06-01 09:00", "2024-06-01 10:00"]
# Wrong if these were New York local times: pandas assumes UTC.
as_utc = pd.to_datetime(naive_strings, utc=True)
# Right: parse as naive, then attach the real zone.
as_local = pd.to_datetime(naive_strings).tz_localize("America/New_York")
print(as_local) # 09:00-04:00 and 10:00-04:00Use utc=True only when the strings carry offsets, or when you know the naive ones are UTC.
Explicit formats and bad values
When the layout is fixed, give format. The %z directive reads an offset such as +0100 or +01:00. A format also makes parsing faster and removes ambiguity.
import pandas as pd
values = ["2024-01-01 00:00 +0100", "bad value"]
out = pd.to_datetime(values, format="%Y-%m-%d %H:%M %z", utc=True, errors="coerce")
print(out) # 2023-12-31 23:00:00+00:00 and NaTWith errors="coerce", unparseable entries become NaT instead of raising. Count the NaT values afterward, because coercion hides data problems.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| Column has `object` dtype | Mixed offsets without `utc=True` | Pass `utc=True` |
| `FutureWarning` about mixed time zones | Same cause in recent pandas | Pass `utc=True` |
| Times are shifted by hours | Naive local strings parsed with `utc=True` | Parse naive, then `tz_localize` |
| `TypeError` when comparing | One side is naive, one is aware | Localize both the same way |
Exercise
The list ["2024-03-01T12:00:00+05:30", "2024-03-01T12:00:00-04:00"] holds two strings with different offsets. Which call parses it into one tz-aware column, and what are the two resulting UTC times?
Show answer
pd.to_datetime(values, utc=True). Subtracting +05:30 from 12:00 gives 06:30 UTC, and subtracting -04:00 (adding 4 hours) gives 16:00 UTC. The result is 2024-03-01 06:30:00+00:00 and 2024-03-01 16:00:00+00:00.