Sub topic 2
UTC, offsets and IANA zone names
This chapter assumes you know the difference between naive and aware timestamps from the previous chapter. It also assumes you can read basic pandas code. Here you will learn the three ways a time zone gets described: UTC, a fixed offset, and a named zone from the IANA database. You need to tell them apart before you attach or convert zones in later chapters.
UTC is the reference point
UTC (Coordinated Universal Time) is the time standard that every other zone is defined against. It has no daylight saving time, and it never jumps. A UTC timestamp identifies one moment, and everyone reads it the same way.
Every tz-aware timestamp in pandas is stored as a count of nanoseconds since 1970-01-01 00:00 UTC. The zone is stored next to that number. The zone only affects how the wall-clock time is displayed and which calendar day the instant falls on.
import pandas as pd
a = pd.Timestamp("2024-01-01 00:00", tz="Europe/Paris")
b = a.tz_convert("UTC")
c = a.tz_convert("Asia/Tokyo")
print(a) # 2024-01-01 00:00:00+01:00
print(b) # 2023-12-31 23:00:00+00:00
print(c) # 2024-01-01 08:00:00+09:00
print(a.value == b.value == c.value) # TrueThe three printed values show different wall-clock times, but they are one instant. For this reason, UTC is the usual choice for storing and exchanging timestamps. You convert to a local zone only when a person needs to read the value or when you need local calendar days.
Offsets and zone names are different things
An offset is a fixed number of hours and minutes added to UTC, such as +02:00 or -05:00. A zone name such as America/New_York identifies a region and a set of rules. Those rules say which offset applies at each moment.
| Property | Fixed offset | IANA zone name |
|---|---|---|
| Example | `-05:00` | `America/New_York` |
| Offset over a year | Always the same | Can change, for example for DST |
| Knows about DST | No | Yes |
| Knows about past rule changes | No | Yes |
| Good for | Recording what a source reported | Local-time logic and calendars |
The same name gives different offsets in different seasons. Compare two Mountain-time regions of the United States. Denver observes DST. Phoenix does not.
import pandas as pd
for tz in ["America/Phoenix", "America/Denver"]:
for date in ["2024-01-15 12:00", "2024-07-15 12:00"]:
ts = pd.Timestamp(date, tz=tz)
print(tz, date, ts.strftime("%z"), ts.tzname())The output is:
America/Phoenix 2024-01-15 12:00 -0700 MST
America/Phoenix 2024-07-15 12:00 -0700 MST
America/Denver 2024-01-15 12:00 -0700 MST
America/Denver 2024-07-15 12:00 -0600 MDTIn January the two zones share an offset, so an offset alone cannot tell them apart. In July they differ by an hour. If you keep only -07:00, you lose the information needed to predict the summer value.
The strftime("%z") call gives the offset in effect at that instant. The tzname() call gives the abbreviation, such as MST. Use the offset for calculations and treat the abbreviation as display text.
Why abbreviations are not zone identifiers
Abbreviations such as IST or CST are not unique. IST can mean India, Israel or Irish Standard Time. CST can mean North American Central, China or Cuba Standard Time. Different zones also share an abbreviation for different offsets. Do not use abbreviations to attach a zone to data, and avoid parsing them from strings when you can get an offset or an IANA name instead.
The IANA time zone database
The IANA time zone database (also called tz, tzdata or the Olson database) is the shared catalog of zone names and their rules. Operating systems, Python and most programming languages use it. Each entry is named Area/Location, such as Europe/Berlin, Asia/Kolkata or America/Argentina/Buenos_Aires. The location is usually a major city, chosen because a city's clock history is easier to record than a country's.
You can check which names your Python installation knows:
from zoneinfo import available_timezones
names = available_timezones()
print("America/New_York" in names) # True
print("Asia/Kolkata" in names) # True
print("America/NewYork" in names) # Falsezoneinfo is in the standard library from Python 3.9. On Windows there is no system copy of the database. Install the tzdata package (pip install tzdata) so that Python and pandas can find the names.
A misspelled name raises an error when you attach it. The exception class depends on the backend, so catching Exception is the portable form:
import pandas as pd
try:
pd.Timestamp("2024-01-01", tz="America/NewYork")
except Exception as e:
print("unknown zone:", type(e).__name__ != "")The database holds history
A zone name also covers the past. Governments change rules, and the database records each change with its date. Moscow is a good example. It used a permanent +04:00 offset between 2011 and 2014, and it has used +03:00 since.
import pandas as pd
for date in ["2012-01-15 12:00", "2024-01-15 12:00"]:
ts = pd.Timestamp(date, tz="Europe/Moscow")
print(date, ts.strftime("%z"))The output is +0400 for 2012 and +0300 for 2024. A fixed offset cannot do this. If you store +03:00 for Moscow, every old timestamp is wrong by an hour.
The database changes over time
Rules for the future also change. Governments announce DST changes, sometimes with little notice, and the IANA project publishes new releases. Which release you get depends on where pandas reads its data:
- With the
zoneinfobackend, pandas reads the system database or thetzdatapackage. - With
pytz, pandas reads the copy bundled with that package.
Timestamps already stored as UTC stay correct. Local wall-clock times for dates after a rule change will be wrong until you update the data source. Keep tzdata (and pytz, if you use it) current in long-lived environments. A later chapter covers the backends in more detail.
Fixed offsets in pandas
You can create a fixed-offset zone with datetime.timezone. pandas accepts it wherever it accepts a zone:
from datetime import timedelta, timezone
import pandas as pd
fixed = timezone(timedelta(hours=-5))
ts = pd.Timestamp("2024-07-15 09:30", tz=fixed)
print(ts) # 2024-07-15 09:30:00-05:00
print(ts.tz_convert("UTC")) # 2024-07-15 14:30:00+00:00Fixed offsets are useful when the source only gives an offset and you have no way to learn the region. They are also what pandas creates when it parses strings like 2024-07-15T09:30:00-05:00 without utc=True. A later chapter covers that case.
The zone "UTC" is the one fixed zone you will use constantly. Pass tz="UTC" or utc=True to get it.
The Etc/GMT sign trap
The database includes fixed-offset names of the form Etc/GMT+N. Their signs are reversed compared with the usual convention. This follows the POSIX standard.
import pandas as pd
print(pd.Timestamp("2024-07-15 09:30", tz="Etc/GMT+5")) # 2024-07-15 09:30:00-05:00
print(pd.Timestamp("2024-07-15 09:30", tz="Etc/GMT-5")) # 2024-07-15 09:30:00+05:00Etc/GMT+5 means five hours behind UTC, which is -05:00. If you need a fixed offset, a timezone(timedelta(...)) object is easier to read and less error-prone.
Choosing between them
Use these rules when deciding how to describe a zone:
- Store and exchange instants in UTC.
- Use an IANA name when you need local calendar logic. Examples are "all rows for March 10 in New York", daily totals, business hours and scheduling.
- Use a fixed offset only when it is all you have, and treat the result as a record of what the source said.
- Do not turn an offset into a region guess.
-05:00in January fits New York, but it also fits Chicago in summer and Bogota all year.
Converting a fixed-offset value to a named zone with tz_convert is correct, because the instant is unambiguous. You then get that region's DST rules from that point on:
import pandas as pd
ts = pd.Timestamp("2024-07-15T09:30:00-05:00")
print(ts.tz_convert("America/New_York")) # 2024-07-15 10:30:00-04:00
print(ts.tz_convert("America/Chicago")) # 2024-07-15 09:30:00-05:00Both results describe the same instant. The first one is an hour later on the wall clock because New York is on daylight time and the source offset was -05:00.
Common mistakes
- Treating
-05:00asAmerica/New_York. The two agree only in winter. - Using three-letter abbreviations as zone identifiers.
- Reading
Etc/GMT+5as+05:00. - Forgetting
tzdataon Windows and getting "no time zone found" errors. - Assuming old dates follow today's rules. Check the offset with
strftime("%z")if a historical result looks an hour off.
Exercise
A source gives you the timestamp "2024-07-01 08:00" and the offset -06:00, with no region. A colleague says to store it as America/Chicago. Is that safe, and what should you store?
Show answer
It is not safe to assume the region. -06:00 in July matches Mexico City, Costa Rica and others as well as being the winter offset for Chicago. Store the instant in UTC (2024-07-01 14:00 UTC) and keep the offset as given. Attach a named zone only when you learn the real region.
Exercise
What do pd.Timestamp("2024-07-15 12:00", tz="Etc/GMT-3") and pd.Timestamp("2024-07-15 12:00", tz="Etc/GMT+3") print, and why?
Show answer
The first prints 2024-07-15 12:00:00+03:00 and the second prints 2024-07-15 12:00:00-03:00. The Etc/GMT names use the POSIX sign convention, which is the opposite of the usual one, so Etc/GMT-3 is three hours ahead of UTC.