About Lesson
Handling time zones is another crucial aspect of working with date and time data. Pandas allows you to localize a Timestamp
to a specific time zone and convert between time zones.
# Localizing a Timestamp to a specific time zone
timestamp = pd.to_datetime('2024-11-30 14:30:00')
localized_timestamp = timestamp.tz_localize('UTC')
print(f"Localized Timestamp (UTC): {localized_timestamp}")
# Convert the timestamp to a different time zone
converted_timestamp = localized_timestamp.tz_convert('US/Eastern')
print(f"Converted Timestamp (US/Eastern): {converted_timestamp}")
Output:
Localized Timestamp (UTC): 2024-11-30 14:30:00+00:00
Converted Timestamp (US/Eastern): 2024-11-30 09:30:00-05:00
This chapter introduced a variety of methods and functions for handling date and time data in Pandas, from creating Timestamp
objects to performing advanced time series operations like resampling, shifting, and rolling calculations. Mastery of date and time manipulation is essential when working with time series data, making Pandas an invaluable tool for any data analyst.