Python | Pandas DataFrame.tz_convert

30 May, 2022
Pandas DataFrame is a two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). Arithmetic operations align on both row and column labels. It can be thought of as a dict-like container for Series objects. This is the primary data structure of the Pandas. Pandas DataFrame.tz_convert() function is used Convert tz-aware axis to target time zone.
Syntax: DataFrame.tz_convert(tz, axis=0, level=None, copy=True) Parameter : tz : string or pytz.timezone object axis : the axis to convert level : If axis is a MultiIndex, convert a specific level. Otherwise must be None copy : Also make a copy of the underlying data Returns : tz converted dataframe
Example #1: Use DataFrame.tz_convert() function to convert the timezone of the given dataframe. 
Python3
# importing pandas as pdimport pandas as pd # Creating the DataFramedf = pd.DataFrame({'Weight':[45, 88, 56, 15, 71],                   'Name':['Sam', 'Andrea', 'Alex', 'Robin', 'Kia'],                   'Age':[14, 25, 55, 8, 21]}) # Create the indexindex_ = pd.date_range('2010-10-09 08:45', periods = 5, freq ='H', tz = 'US / Central') # Set the indexdf.index = index_ # Print the DataFrameprint(df)
Output :  Now we will use DataFrame.tz_convert() function to convert the time zone of the given dataframe to ‘Europe/Berlin’. 
Python3
# Let's find out the current timezone# of the given dataframeprint(df.index) # Let's convert the timezone of the# dataframe to 'Europe / Berlin'df = df.tz_convert(tz = 'Europe / Berlin') # Let's find out the current timezone# of the given dataframeprint(df.index)
Output :   As we can see in the output, the DataFrame.tz_convert() function has successfully converted the timezone of the given dataframe to the desired timezone.   Example #2 : Use DataFrame.tz_convert() function to convert the timezone of the given dataframe. The Index of the given dataframe is a MultiIndex. 
Python3
# importing pandas as pdimport pandas as pd # Creating the DataFramedf = pd.DataFrame({'Weight':[45, 88, 56, 15, 71],                   'Name':['Sam', 'Andrea', 'Alex', 'Robin', 'Kia'],                   'Age':[14, 25, 55, 8, 21]}) # Create the MultiIndexindex_ = 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 indexdf.index = index_ # Print the DataFrameprint(df)
Output :  Now we will use DataFrame.tz_convert() function to convert the time zone of the Level 1 of the MultiIndex in the given dataframe to ‘Europe/Berlin’. 
Python3
# Let's find out the current timezone# of the Level 1 of the given dataframeprint(df.index[1]) # Let's convert the timezone of the# level 1 of the dataframe to 'Europe / Berlin'df = df.tz_convert(tz = 'Europe/Berlin', level = 1) # Let's find out the current timezone# of the level 1 of the given dataframeprint(df.index[1])
Output :   As we can see in the output, the DataFrame.tz_convert() function has successfully converted the timezone of the desired level in the given dataframe to the desired timezone.
vinayedula
Python pandas-dataFrame
Python pandas-dataFrame-methods
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.