Sub topic 4
Converting between zones with tz_convert
This chapter assumes you can already create tz-aware data with tz_localize, and that you know what UTC offsets and IANA zone names such as America/New_York are. Here you will learn what tz_convert does, where it works, and which errors it raises.
What conversion means
tz_localize attaches a zone to a naive timestamp. It answers the question "which zone was this wall-clock time recorded in?" tz_convert works on data that already has a zone. It re-expresses the same instant in another zone. The underlying UTC moment does not change. Only the displayed wall-clock time and offset change.
Converting a single Timestamp
import pandas as pd
ts = pd.Timestamp("2024-03-10 12:00")
print(ts.tzinfo) # None, so this timestamp is naive
ts_utc = ts.tz_localize("UTC")
print(ts_utc) # 2024-03-10 12:00:00+00:00
print(ts_utc.tz_convert("America/New_York")) # 2024-03-10 08:00:00-04:00The converted value is equal to the original. Comparing them with == returns True because both point to the same instant. The offset is -04:00 because New York had already moved to daylight time that morning.
Converting a Series
A Series of datetimes has no tz_convert method of its own. Use the .dt accessor instead. The example below localizes first, then converts.
import pandas as pd
s = pd.Series(pd.to_datetime(["2024-01-15 09:30", "2024-07-15 09:30"]))
s_ny = s.dt.tz_localize("America/New_York")
s_utc = s_ny.dt.tz_convert("UTC")
print(s_ny.dtype) # datetime64[ns, America/New_York]
print(s_utc) # 14:30 UTC in January, 13:30 UTC in JulyBoth rows show 09:30 in New York, but the UTC results differ by an hour. New York is UTC-5 in January and UTC-4 in July. tz_convert applies the correct offset for each individual timestamp, so you do not need to handle DST yourself.
Converting a DataFrame index
When the time information lives in the index, call tz_convert directly on the DataFrame. It converts the index and leaves the columns alone.
import pandas as pd
idx = pd.date_range("2024-03-10 12:00", periods=3, freq="h", tz="UTC")
df = pd.DataFrame({"value": [1, 2, 3]}, index=idx)
df_tokyo = df.tz_convert("Asia/Tokyo")
print(df_tokyo.index) # 21:00, 22:00 and 23:00 at +09:00If the index is a DatetimeIndex, idx.tz_convert(...) does the same job. If the datetimes are in a column, use df["col"].dt.tz_convert(...).
The naive-data error
tz_convert refuses to guess a zone for naive data.
import pandas as pd
naive = pd.Timestamp("2024-03-10 12:00")
try:
naive.tz_convert("UTC")
except TypeError as e:
print(e) # Cannot convert tz-naive Timestamp, use tz_localize to localizeThe error is deliberate. Converting requires a starting zone, and only you know which zone the data was recorded in. Localize first, then convert.
The reverse mistake is less visible. Calling tz_localize on data that is already aware raises a TypeError, and the message tells you to use tz_convert. As a rule, use tz_localize when the data has no zone and tz_convert when it has one.
Converting to None
Passing None converts the data to UTC and then drops the zone, which leaves naive UTC values.
import pandas as pd
ts_ny = pd.Timestamp("2024-07-15 09:30", tz="America/New_York")
print(ts_ny.tz_convert(None)) # 2024-07-15 13:30:00The result is naive, but its values are UTC, not local time. Keep that in mind when you share the data. If you want to keep the local wall-clock values instead, use tz_localize(None). Chapter 11 covers that choice in more detail.
Summary of the methods
| Situation | Method | Effect on the instant |
|---|---|---|
| Naive data, zone known | `tz_localize("Zone")` | Fixes the instant from the wall-clock time |
| Aware data, new zone wanted | `tz_convert("Zone")` | Instant unchanged |
| Aware data, want naive UTC | `tz_convert(None)` | Instant unchanged, zone dropped |
| Aware data, want naive local | `tz_localize(None)` | Wall-clock time kept, zone dropped |
Exercise
A Series s holds naive timestamps that were logged in UTC. You need them as Tokyo local time. Which two calls do you make, in what order, and why does calling tz_convert first fail?
Show answer
Call s.dt.tz_localize("UTC") first, then .dt.tz_convert("Asia/Tokyo"). The naive data has no zone, so tz_convert has no starting point and raises a TypeError. Localizing to UTC states which zone the logged values were in.
Exercise
What does pd.Timestamp("2024-01-15 09:30", tz="America/New_York").tz_convert("UTC") print, and why is the result different from a July timestamp at the same wall-clock time?
Show answer
It prints 2024-01-15 14:30:00+00:00. In January New York is on standard time (UTC-5). In July it is on daylight time (UTC-4), so 09:30 there converts to 13:30 UTC.