Time zones
Timestamps can be tied to a time zone so you can convert between regions correctly.
In this page:
Syntax
s = s.tz_localize("Zone/Name")
s = s.tz_convert("Other/Zone")
Time zones
Naive timestamps have no zone. tz_localize attaches a zone to naive times, and tz_convert changes an aware time to another zone. Comparing aware and naive timestamps raises an error, so be consistent. Store data in UTC and convert for display.
Note:
Use UTC internally and only convert for presentation.
Example: Time zones
import pandas as pd
ts = pd.Timestamp("2024-06-01 12:00")
utc = ts.tz_localize("UTC")
print(utc)
print(utc.tz_convert("Asia/Tokyo"))
# Output:
# 2024-06-01 12:00:00+00:00
# 2024-06-01 21:00:00+09:00
Related Topics
Common Mistakes
- Using tz_localize on already-aware data
- Comparing naive with aware datetimes
- Ignoring daylight saving shifts
Chapter Summary
- tz_localize attaches a zone
- tz_convert switches zones
- Store in UTC
- Do not mix naive and aware
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: