← Back to Pandas Course | Chapter 10: Time Series | Lesson 7 of 7

Time zones

Timestamps can be tied to a time zone so you can convert between regions correctly.

In this page:

  1. Time zones
Syntax
python
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

python
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
  1. Using tz_localize on already-aware data
  2. Comparing naive with aware datetimes
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.