Sub topic 2
UTC, offsets and IANA zone names
This chapter assumes you know basic Python, pandas Series and DataFrames, and the difference between naive and aware timestamps from the previous chapter. Here you will learn the three ways pandas identifies a time zone and when to use each one.
UTC is the common reference
UTC (Coordinated Universal Time) is the reference clock that all other zones are defined against. It has no daylight saving time, so one UTC reading names exactly one moment. Two timestamps in UTC can be compared, sorted and subtracted without any zone rules.
A time zone changes only how an instant is displayed. The instant stays the same.
Offsets describe one moment
A UTC offset is the difference between local time and UTC at a given moment, written like -05:00 or +05:30. The offset belongs to a single timestamp. It does not tell you which place the timestamp came from, or what the offset will be next month.
You can read the offset from an aware timestamp with strftime("%z"):
import pandas as pd
ts = pd.Timestamp("2024-01-15 12:00", tz="UTC")
local = ts.tz_convert("America/New_York")
print(local) # 2024-01-15 07:00:00-05:00
print(local.strftime("%z")) # -0500
print(local.tzname()) # ESTAn offset of -05:00 fits many places. New York uses it in winter. Lima uses it all year. A timestamp that carries only an offset cannot tell you what the clock will read in six months.
IANA names carry the rules
The IANA time zone database (also called tzdata or the Olson database) is the shared catalogue of the world's zones. Each entry has a name of the form Area/Location, such as America/New_York, Europe/Berlin or Asia/Kolkata. An entry stores the zone's full history of offsets and daylight saving rules.
An IANA name therefore stands for a function. Given a UTC instant, it returns the correct offset for that place at that time.
import pandas as pd
zones = ["America/New_York", "Europe/Berlin", "Asia/Kolkata"]
for when in ["2024-01-15 12:00", "2024-07-15 12:00"]:
utc = pd.Timestamp(when, tz="UTC")
for zone in zones:
print(utc, "->", zone, utc.tz_convert(zone))The same UTC hour gives New York 07:00-05:00 in January and 08:00-04:00 in July. Berlin moves from +01:00 to +02:00. Kolkata stays at +05:30 because India has no daylight saving time. Only the zone name lets pandas pick the right offset each time.
Fixed offsets as zones
Sometimes you only have an offset, for example from a sensor that always reports UTC+2. You can pass a datetime.timezone object as the zone. pandas then applies the same offset to every timestamp:
from datetime import timedelta, timezone
import pandas as pd
minus_five = timezone(timedelta(hours=-5))
winter = pd.Timestamp("2024-01-15 12:00", tz="UTC").tz_convert(minus_five)
summer = pd.Timestamp("2024-07-15 12:00", tz="UTC").tz_convert(minus_five)
print(winter) # 2024-01-15 07:00:00-05:00
print(summer) # 2024-07-15 07:00:00-05:00New York's wall clock in July reads 08:00, so this fixed offset is wrong for New York in summer. Use a fixed offset only when the source really has no daylight saving time.
Choosing a representation
| Representation | Example | Knows DST rules | Good for |
|---|---|---|---|
| UTC | `"UTC"` | Not needed | Storing, comparing and logging instants |
| Fixed offset | `timezone(timedelta(hours=2))` | No | Sources that never change offset |
| IANA name | `"Europe/Berlin"` | Yes | Showing or grouping by local time |
A common pattern is to store and compute in UTC, and convert to an IANA zone only when you display data or group it by local day. The next chapters build on this with tz_localize and tz_convert.
Common errors
- A misspelled or unknown name, such as
"US/Newyork", raises an error from the time zone backend. Copy names from the IANA list instead of guessing. - Treating a
-05:00offset as proof that a place is New York. The offset fits many zones and says nothing about daylight saving time. - Using abbreviations such as
ESTorISTas zone names. They are ambiguous (ISTcan mean India, Ireland or Israel), so useArea/Locationnames.
Exercise
A colleague stores 2024-07-15 08:00:00-05:00 and says this means New York time. What is wrong with that claim, and what should you store so that you can recover local New York time later?
Show answer
The offset -05:00 is only the offset at that moment. New York is at -04:00 in July, so this value is not New York local time, and other places also use -05:00. Store the instant in UTC (here 13:00 UTC) together with the IANA name America/NewYork. Then tzconvert("America/New_York") returns the correct local time for any date.