Handling Time Zones in Pandas

Dealing with time zones in pandas is like ensuring everyone shows up to the global meeting at the right hour. Let’s sync our watches.

Pandas provides straightforward methods to make your datetime data aware of time zones and to convert between them, keeping your global data in perfect harmony:

import pandas as pd

# Create a timezone-naive datetime series
dt_series = pd.Series(pd.date_range('2024-03-15 08:00:00', periods=3, freq='H'))

# Make it timezone-aware by setting to UTC
dt_series = dt_series.dt.tz_localize('UTC')

# Convert to another timezone, e.g., EST
dt_series_est = dt_series.dt.tz_convert('America/New_York')
print(dt_series_est)

This code takes a naive datetime series, assigns it a UTC timezone, and then shifts it to Eastern Standard Time (EST), keeping your data aligned with the sun’s dance across the globe.

Leave a Reply