Open In App

Pandas DataFrame.tz_convert

Last Updated : 29 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Pandas, DataFrame.tz_convert() function allows for easy conversion of timezone-aware datetime indices in a DataFrame to a specified target time zone. This feature is especially useful when dealing with data collected across different time zones.

Syntax: DataFrame.tz_convert(tz, axis=0, level=None, copy=True)

Parameters:

  • tz: A string or pytz.timezone object specifying the target time zone.
  • axis: The axis to convert, either 0 (rows) or 1 (columns).
  • level: If the axis is a MultiIndex, specify the level to convert. Otherwise, it must be None.
  • copy: Whether to return a new object or modify the original one.

Returns a DataFrame with the datetime index converted to the specific time zone.

Convert Timezone for a Simple DataFrame using .tz_convert()

This example demonstrates how to convert the time zone of a DataFrame with a datetime index.

Python
# importing pandas as pd
import pandas as pd

# Creating the DataFrame
df = pd.DataFrame({'Weight': [45, 88, 56, 15, 71],
                   'Name': ['Sam', 'Andrea', 'Alex', 'Robin', 'Kia'],
                   'Age': [14, 25, 55, 8, 21]})

# Create the datetime index
index_ = pd.date_range('2010-10-09 08:45', periods=5, freq='H', tz='US/Central')

# Set the datetime index
df.index = index_

# Print the DataFrame
print("DataFrame:")
print(df)

# Convert the timezone to 'Europe/Berlin'
df = df.tz_convert(tz='Europe/Berlin')

# Print the DataFrame after timezone conversion
print("\nDataFrame after timezone conversion:")
print(df)

Output:

Convert-Timezone-for-a-Simple-DataFrame

DataFrame after TimeZone Conversion

The code creates a simple DataFrame with a datetime index set to US/Central. After printing the initial DataFrame, we use tz_convert() to change the time zone to Europe/Berlin. The converted DataFrame will now display the datetimes in the new time zone.

Convert Timezone in a MultiIndex DataFrame using tz_convert()

In this example, we show how to use tz_convert() with a MultiIndex, where we convert a specific level’s time zone.

Python
# importing pandas as pd
import pandas as pd

# Creating the DataFrame
df = pd.DataFrame({'Weight': [45, 88, 56, 15, 71],
                   'Name': ['Sam', 'Andrea', 'Alex', 'Robin', 'Kia'],
                   'Age': [14, 25, 55, 8, 21]})

# Create a MultiIndex
index_ = pd.MultiIndex.from_product([['Date'], pd.date_range('2010-10-09 08:45', periods=5, freq='H', tz='US/Central')],
                                     names=['Level 1', 'Level 2'])

# Set the MultiIndex
df.index = index_

# Print the DataFrame
print("DataFrame:")
display(df)

# Convert the timezone of Level 2 of the MultiIndex to 'Europe/Berlin'
df = df.tz_convert(tz='Europe/Berlin', level=1)

# Print the DataFrame after timezone conversion
print("\nDataFrame after timezone conversion to 'Europe/Berlin':")
display(df)

Output:

Convert-Timezone-in-a-MultiIndex-DataFrame

In this case, the DataFrame uses a MultiIndex, with the second level (Level 2) containing datetime values. The tz_convert() function is applied to only the Level 2 index, converting its time zone to Europe/Berlin.



Similar Reads