Python Pandas - Handling Time Zone



Handling time zones is crucial for data analysis, particularly when dealing with timestamps across different time zones. Pandas provides rich support for working with timestamps in different time zones with the help of the libraries like pytz and dateutil or datetime.timezone objects from the standard library.

By default, pandas timestamps are time zone-unaware (naive). To work with time zones, pandas offers methods to localize timestamps to a specific time zone or convert timestamps between different time zones.

In this tutorial we will learn about how to handle time zones in Pandas, including creating, converting, localizing time zone-aware data and removing time zone information.

Creating Time Zone-Unaware Timestamps

When you create a timestamp in pandas, it is usually time zone-unaware, meaning that it does not reference any specific time zone.

Example

The following example verifies the time zone information of a Pandas timestamp object using the tz keyword.

import pandas as pd

# Creating a naive Timestamp
ts = pd.Timestamp(2025, 1, 1, 12)
print('Input Timestamp:', ts)

# Verify the time zone information
print("Time Zone Information:", ts.tz)  

Following is the output of the above code −

Input Timestamp: 2025-01-01 12:00:00
Time Zone Information: None

In the above output you can see that the time zone information of a timestamp is None, meaning that the timestamp have no associated time zone.

Localizing Dates to a Time Zone

To assign a time zone to a naive (time zone-unaware) date, you can use the tz_localize() method, or the tz parameter in the date_range() function, Timestamp(), or DatetimeIndex() constructors.

Example: Assign a Particular Time Zone using tz Parameter

The following example demonstrates creating a timestamp with a specific time zone using the tz parameter in Timestamp() constructors.

import pandas as pd

# Creating a Timestamp with a time zone
ts = pd.Timestamp(2025, 1, 1, 12, tz='Australia/Brisbane')

# display the Timestamp
print("Input Timestamp:", ts)

# Verify the time zone information
print("Time Zone Information:", ts.tz)  

Following is the output of the above code −

Input Timestamp: 2025-01-01 12:00:00+10:00
Time Zone Information: Australia/Brisbane

Example: Localizing Dates using tz_localize() Method

This example uses the tz_localize() method on created Pandas dates object to localize the time zone.

import pandas as pd

# Creating a naive DatetimeIndex
datetime_indx = pd.date_range("2024-01-01", periods=5, freq="D")

# display the DatetimeIndex
print("Input DatetimeIndex:")
print(datetime_indx)

# Localize to a specific time zone
localized = datetime_indx.tz_localize("Europe/London")
print("\nDatetimeIndex after Localized to a time zone:")
print(localized)

Following is the output of the above code −

Input DatetimeIndex:
DatetimeIndex(['2024-01-01', '2024-01-02', '2024-01-03', '2024-01-04',
               '2024-01-05'],
              dtype='datetime64[ns]', freq='D')

DatetimeIndex after Localized to a time zone:
DatetimeIndex(['2024-01-01 00:00:00+00:00', '2024-01-02 00:00:00+00:00',
               '2024-01-03 00:00:00+00:00', '2024-01-04 00:00:00+00:00',
               '2024-01-05 00:00:00+00:00'],
              dtype='datetime64[ns, Europe/London]', freq=None)

Localizing Time Zones with pytz and dateutil libraries

You can also specify the time zones using pytz or dateutil libraries directly.

Example: Using pytz library for localization

Following example uses the pytz library for localizing the Pandas dates object time zone.

import pandas as pd
from pytz import timezone

# Creating a naive DatetimeIndex
datetime_indx = pd.date_range("2024-01-01", periods=5, freq="D")

# display the DatetimeIndex
print("Input DatetimeIndex:")
print(datetime_indx)

# Using pytz for localization
tz = timezone("US/Eastern")

# Localize to a specific time zone
localized = datetime_indx.tz_localize(tz)
print("\nDatetimeIndex after Localized to a time zone:")
print(localized)

Following is the output of the above code −

Input DatetimeIndex:
DatetimeIndex(['2024-01-01', '2024-01-02', '2024-01-03', '2024-01-04',
               '2024-01-05'],
              dtype='datetime64[ns]', freq='D')

DatetimeIndex after Localized to a time zone:
DatetimeIndex(['2024-01-01 00:00:00-05:00', '2024-01-02 00:00:00-05:00',
               '2024-01-03 00:00:00-05:00', '2024-01-04 00:00:00-05:00',
               '2024-01-05 00:00:00-05:00'],
              dtype='datetime64[ns, US/Eastern]', freq=None)

Example: Using dateutil library for localization

Following example uses the dateutil library for localizing the Pandas dates object time zone.

from dateutil import tz
import pandas as pd

# Creating a naive DatetimeIndex
datetime_indx = pd.date_range("2024-01-01", periods=5, freq="D")

# display the DatetimeIndex
print("Input DatetimeIndex:")
print(datetime_indx)

# Using dateutil for localization
localized = datetime_indx.tz_localize("dateutil/US/Eastern")

print("\nDatetimeIndex after Localized to a time zone:")
print(localized)

Following is the output of the above code −

Input DatetimeIndex:
DatetimeIndex(['2024-01-01', '2024-01-02', '2024-01-03', '2024-01-04',
               '2024-01-05'],
              dtype='datetime64[ns]', freq='D')

DatetimeIndex after Localized to a time zone:
DatetimeIndex(['2024-01-01 00:00:00-05:00', '2024-01-02 00:00:00-05:00',
               '2024-01-03 00:00:00-05:00', '2024-01-04 00:00:00-05:00',
               '2024-01-05 00:00:00-05:00'],
              dtype='datetime64[ns, tzfile('/usr/share/zoneinfo/US/Eastern')]', freq=None)

Converting Between Time Zones

To convert a time-aware object from one time zone to another, you can use the tz_convert() method. This is particularly useful for comparing events across different time zones.

Example

This example demonstrates using the tz_convert() method for converting the time zone of the pandas time-aware DatetimeIndex object from Europe/London to US/Eastern.

import pandas as pd

# Creating a DatetimeIndex
datetime_indx = pd.date_range("2024-01-01", periods=5, freq="D", tz="Europe/London")

# display the DatetimeIndex
print("Input DatetimeIndex:")
print(datetime_indx)

# Convert from Europe/London to US/Eastern time zone
converted_rng = datetime_indx.tz_convert("US/Eastern")

print("\nDatetimeIndex after converted to the 'US/Eastern' time zone:")
print(converted_rng)

Following is the output of the above code −

Input DatetimeIndex:
DatetimeIndex(['2024-01-01 00:00:00+00:00', '2024-01-02 00:00:00+00:00',
               '2024-01-03 00:00:00+00:00', '2024-01-04 00:00:00+00:00',
               '2024-01-05 00:00:00+00:00'],
              dtype='datetime64[ns, Europe/London]', freq='D')

DatetimeIndex after converted to the 'US/Eastern' time zone:
DatetimeIndex(['2023-12-31 19:00:00-05:00', '2024-01-01 19:00:00-05:00',
               '2024-01-02 19:00:00-05:00', '2024-01-03 19:00:00-05:00',
               '2024-01-04 19:00:00-05:00'],
              dtype='datetime64[ns, US/Eastern]', freq='D')

Handling Ambiguous Time Zones with fold

During the shift from daylight saving time (DST) to standard time, certain times may appear twice (e.g., clocks fall back). Pandas provides the fold parameter, which supports specifying ambiguous times during DST transitions. This feature is only available with dateutil time zones.

Example

The following example handling the ambiguous time zones using the fold parameter.

import pandas as pd
import datetime

# Using fold to handle ambiguous times
times = pd.Timestamp(
datetime.datetime(2023, 10, 29, 1, 30, 0), tz="dateutil/Europe/London", fold=1)

print(times)

Following is the output of the above code −

2023-10-29 01:30:00+00:00

To localize an ambiguous datetime with pytz, ambiguity during DST is handled differently, so its recommended to use Timestamp.tz_localize() for full control over DST handling.

Removing Time Zone Information

To remove time zone information from a Pandas dates object, you can use tz_localize(None) or tz_convert(None) methods. Both methods remove the time zone component and converts to either local time or UTC time, respectively.

Example: Using the tz_localize() Method to Remove Time Zone

Th following example uses the tz_localize(None) method to remove the time zone information of a DatetimeIndex object.

import pandas as pd

# Creating a DatetimeIndex
datetime_indx = pd.date_range("2024-01-01", periods=5, freq="D", tz="Europe/London")

# display the DatetimeIndex
print("Input DatetimeIndex:")
print(datetime_indx)

# Remove time zone information with tz_localize
result = datetime_indx.tz_localize(None)
print("\nDatetimeIndex after removing the time zone information:", result)

Following is the output of the above code −

Input DatetimeIndex:
DatetimeIndex(['2024-01-01 00:00:00+00:00', '2024-01-02 00:00:00+00:00',
               '2024-01-03 00:00:00+00:00', '2024-01-04 00:00:00+00:00',
               '2024-01-05 00:00:00+00:00'],
              dtype='datetime64[ns, Europe/London]', freq='D')

DatetimeIndex after removing the time zone information: DatetimeIndex(['2024-01-01', '2024-01-02', '2024-01-03', '2024-01-04',
               '2024-01-05'],
              dtype='datetime64[ns]', freq=None)

Example: Using the tz_convert() Method to Remove Time Zone

Th following example uses the tz_convert(None) method to remove the time zone information of a DatetimeIndex object.

import pandas as pd

# Creating a DatetimeIndex
datetime_indx = pd.date_range("2024-01-01", periods=5, freq="D", tz="Europe/London")

# display the DatetimeIndex
print("Input DatetimeIndex:")
print(datetime_indx)

# Remove time zone information with tz_localize
result = datetime_indx.tz_convert(None)
print("\nDatetimeIndex after removing the time zone information:", result)

Following is the output of the above code −

Input DatetimeIndex:
DatetimeIndex(['2024-01-01 00:00:00+00:00', '2024-01-02 00:00:00+00:00',
               '2024-01-03 00:00:00+00:00', '2024-01-04 00:00:00+00:00',
               '2024-01-05 00:00:00+00:00'],
              dtype='datetime64[ns, Europe/London]', freq='D')

DatetimeIndex after removing the time zone information: DatetimeIndex(['2024-01-01', '2024-01-02', '2024-01-03', '2024-01-04',
               '2024-01-05'],
              dtype='datetime64[ns]', freq='D')
Advertisements