Python Pandas - Extract the timezone from the DateTimeIndex with specific time series frequency

To extract the timezone from a DateTimeIndex with specific time series frequency, use the DateTimeIndex.tz property. This is useful when working with time series data across different geographical regions.

Syntax

The basic syntax to extract timezone information ?

DateTimeIndex.tz

Creating DateTimeIndex with Timezone

First, let's create a DateTimeIndex with timezone information and specific frequency ?

import pandas as pd

# Create DatetimeIndex with period 6 and frequency as D (daily)
# The timezone is Australia/Sydney
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Sydney', freq='D')

# Display DateTimeIndex
print("DateTimeIndex...")
print(datetimeindex)
DateTimeIndex...
DatetimeIndex(['2021-10-20 02:30:50+11:00', '2021-10-21 02:30:50+11:00',
               '2021-10-22 02:30:50+11:00', '2021-10-23 02:30:50+11:00',
               '2021-10-24 02:30:50+11:00', '2021-10-25 02:30:50+11:00'],
              dtype='datetime64[ns, Australia/Sydney]', freq='D')

Extracting Timezone Information

Now we can extract both the timezone and frequency information ?

import pandas as pd

# Create DatetimeIndex with timezone
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Sydney', freq='D')

# Display frequency information
print("DateTimeIndex frequency:")
print(datetimeindex.freq)

# Extract the timezone
print("\nTimezone information:")
print(datetimeindex.tz)
DateTimeIndex frequency:
<Day>

Timezone information:
Australia/Sydney

Working with Different Timezones

You can create DateTimeIndex with various timezones and frequencies ?

import pandas as pd

# Different timezone examples
utc_index = pd.date_range('2021-10-20', periods=3, tz='UTC', freq='H')
us_index = pd.date_range('2021-10-20', periods=3, tz='US/Eastern', freq='2H')

print("UTC Timezone:", utc_index.tz)
print("US Eastern Timezone:", us_index.tz)
print("US Eastern Frequency:", us_index.freq)
UTC Timezone: UTC
US Eastern Timezone: US/Eastern
US Eastern Frequency: <2 * Hours>

Conclusion

The DateTimeIndex.tz property provides easy access to timezone information from pandas DateTimeIndex objects. This is essential for time series analysis involving multiple time zones and helps maintain data integrity across different geographical regions.

Updated on: 2026-03-26T17:08:26+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements