Sub topic 1
Naive and aware timestamps
Prerequisites
This chapter assumes you know basic Python, the pandas Series and DataFrame types, and common datetime ideas such as dates, times and timedeltas. You do not need prior knowledge of time zones. Later chapters cover the zone database, conversion and daylight saving time (DST) in detail.
Two kinds of timestamp
A pandas timestamp is either naive or aware.
- A naive timestamp stores a date and a clock reading, such as
2024-03-10 12:00. It does not say which place that clock reading belongs to. - An aware timestamp stores the same date and clock reading plus a time zone. Together they identify one exact instant on the global timeline.
A naive value of 12:00 could mean noon in Tokyo or noon in Lima. Those moments are 14 hours apart. Pandas cannot tell which you meant, so it treats the value as a plain label.
You can check which kind you have by looking at the tzinfo attribute. It is None for a naive 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.tzinfo) # UTCThe call to tz_localize attached UTC to the naive value. The clock reading stayed 12:00, and the timestamp became aware. The chapter on tz_localize explains this step fully. Here it only shows the difference between the two kinds.
Why the difference matters
Naive data is fine while everything comes from one place and you never compare it with data from elsewhere. Problems start when sources meet. Server logs may be in UTC, a sales file may be in local store time, and a user's browser may send its own offset. If all of these are naive, nothing in the data records the difference, and you must remember it yourself.
Aware data carries that information. Pandas can compare two aware timestamps from different zones correctly, because each one maps to a single instant.
The table summarizes the contrast.
| Property | Naive | Aware |
|---|---|---|
| `tzinfo` | `None` | A zone object |
| Identifies one instant | No | Yes |
| Typical dtype | `datetime64[ns]` | `datetime64[ns, <zone>]` |
| Safe to compare across sources | No | Yes |
Mixing the two fails
Pandas refuses to compare a naive timestamp with an aware one. It raises a TypeError instead of guessing a zone for you.
import pandas as pd
naive = pd.Timestamp("2024-01-01")
aware = pd.Timestamp("2024-01-01", tz="UTC")
try:
naive < aware
except TypeError as e:
print(e) # Cannot compare tz-naive and tz-aware timestampsThis is deliberate. A silent guess would give wrong answers that look plausible. The error forces you to decide which zone the naive values were recorded in.
Subtracting a naive value from an aware one fails for the same reason. So does putting both kinds in one comparison across two columns.
How to decide which kind to use
A reasonable default is to keep data aware while you work with it, and to remove zones only at the edges, such as when you write a file for a tool that cannot read them. The last chapters of this tutorial cover that step.
The following diagram shows the usual path from raw data to a safe comparison.
Checking a whole column
On a Series or DatetimeIndex, the dtype tells you the kind. A naive column has dtype datetime64[ns]. An aware column shows the zone in the dtype.
import pandas as pd
naive = pd.Series(pd.to_datetime(["2024-01-15 09:30", "2024-07-15 09:30"]))
aware = naive.dt.tz_localize("UTC")
print(naive.dtype) # datetime64[ns]
print(aware.dtype) # datetime64[ns, UTC]A column has one zone for all its values. You cannot hold naive and aware values in the same datetime column. If you try to build one from a mix, pandas either raises an error or falls back to the slower object dtype, which stores Python objects one by one. Later chapters show how to avoid that.
Common errors
- Assuming naive means UTC. Naive means "unknown". Treat it as UTC only if you know the source used UTC.
- Comparing across kinds. This raises the
TypeErrorshown above. - Forgetting to check
dtype. Printing a column does not always make the zone obvious. Checkdtypeortzinfofirst.
Exercise
You have a column logged_at with dtype datetime64[ns] and a single timestamp deadline created with tz="UTC". What happens when you run df["logged_at"] < deadline, and what must you find out before fixing it?
Show answer
Pandas raises a TypeError because the column is naive and the deadline is aware. Before fixing it, find out which time zone the logged_at values were recorded in. Then localize the column to that zone so that both sides are aware.