Sub topic 12
Time zone backends: zoneinfo, pytz and dateutil
This chapter assumes you can already use tz_localize and tz_convert and know what an IANA zone name such as America/New_York is. It covers which library supplies the zone rules behind those calls, and which mistakes come from mixing the libraries.
What a backend is
A time zone name is only a label. A backend is a Python library that holds the rules for that label: the UTC offset, the dates of DST changes, and the history of past rule changes. pandas calls the backend whenever it needs an offset.
Three backends matter in practice:
| Backend | Module | Status |
|---|---|---|
| zoneinfo | `zoneinfo` (standard library, Python 3.9+) | Recommended |
| pytz | `pytz` (third party) | Legacy, still common in older code |
| dateutil | `dateutil.tz` (installed with pandas) | Rarely needed |
When you pass a string such as "America/New_York", pandas picks the backend for you. pandas 2.x uses pytz for strings. pandas 3.0 and later use zoneinfo. If your code must behave the same on both, pass tzinfo objects instead of strings.
Passing a backend explicitly
tz_localize, tz_convert and the tz argument accept a string or a tzinfo object. Both give the same result for the same zone.
import pandas as pd
import dateutil.tz
from zoneinfo import ZoneInfo
ts = pd.Timestamp("2024-07-01 12:00")
print(ts.tz_localize("America/New_York"))
print(ts.tz_localize(ZoneInfo("America/New_York")))
print(ts.tz_localize(dateutil.tz.gettz("America/New_York")))
# each line: 2024-07-01 12:00:00-04:00The rules agree, so the instant is the same whichever backend you use. The difference shows up in the tz object itself, which ts.tzinfo and series.dt.tz return. A ZoneInfo object has a .key attribute with the zone name. A pytz object has .zone. Code that reads the name from the tz object must know which backend produced it.
The pytz gotcha
pytz predates zoneinfo and works differently. A pytz zone object is not a single zone. It is a set of fixed-offset variants, and you must call localize to choose one. pandas does this for you inside tz_localize. Plain datetime does not.
from datetime import datetime
from zoneinfo import ZoneInfo
import pytz
wrong = datetime(2024, 7, 1, 12, tzinfo=pytz.timezone("America/New_York"))
right = pytz.timezone("America/New_York").localize(datetime(2024, 7, 1, 12))
modern = datetime(2024, 7, 1, 12, tzinfo=ZoneInfo("America/New_York"))
print(wrong.utcoffset()) # -1 day, 19:04:00 (that is -4:56, the old local mean time)
print(right.utcoffset()) # -1 day, 20:00:00 (that is -4:00)
print(modern.utcoffset()) # -1 day, 20:00:00The wrong line gives an offset of -4:56. That is New York's local mean time from before standard time zones, because pytz takes the first entry in its table. The result is a silent error of 56 minutes. With zoneinfo, the tzinfo= argument works as you would expect.
Fixed offsets are not zones
A fixed offset such as datetime.timezone(timedelta(hours=5)) has no DST rules and no history. It is correct for data that was recorded with a literal +05:00. It is wrong for data that was recorded in a place whose offset changes during the year. Use a named zone for the second kind of data.
Common errors
ZoneInfoNotFoundError: the system has no zone database, which is common on Windows and in slim containers. Runpip install tzdata. pandas liststzdataas a dependency, so this usually appears only with a barezoneinfocall.ModuleNotFoundError: pytz: pandas 3.0 no longer requires pytz. Install it only if your own code imports it.- Unexpected object type: code that checks
isinstance(tz, pytz.BaseTzInfo)fails once strings resolve toZoneInfo. Compare zone names instead.
Choosing a backend
For new code, use zoneinfo, either by passing ZoneInfo(...) objects or by passing strings on pandas 3.0 or later.
Exercise
A colleague writes datetime(2024, 7, 1, 12, tzinfo=pytz.timezone("America/New_York")) and gets an offset of -4:56 instead of -4:00. What went wrong, and what are two ways to fix it?
Show answer
pytz zone objects start with the earliest entry in their table, which is New York's local mean time (-4:56), and tzinfo= does not pick the correct variant. Fix it by calling pytz.timezone("America/New_York").localize(datetime(2024, 7, 1, 12)), or by using ZoneInfo("America/New_York") as the tzinfo argument.