Pandas DataFrame tz_convert() Method


Preparation

Before any data manipulation can occur, one (1) new library will require installation.

  • The Pandas library enables access to/from a DataFrame.

To install this library, navigate to an IDE terminal. At the command prompt ($), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($). Your terminal prompt may be different.

πŸ’‘ Note: The pytz comes packaged with pandas and does not require installation. However, this library is needed for the tz_ localize() and tz_convert() methods to work.

$ pip install pandas

Hit the <Enter> key on the keyboard to start the installation process.

If the installation was successful, a message displays in the terminal indicating the same.


Feel free to view the PyCharm installation guide for the required library.


Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.

import pandas as pd
import pytz

DataFrame tz_convert()

The tz_convert() method converts the time zone of a DataFrame to a different time zone.

To view a list of available time zones, click here or navigate to an IDE and run the following code:

$ print(pytz.all_timezones)

The syntax for this method is as follows:

DataFrame.tz_convert(tz, axis=0, level=None, copy=True)
ParameterDescription
tzThe parameter is a string of a valid time zone. Ex: 'America/Phoenix'.
axisIf zero (0) or index is selected, apply to each column. Default 0.
If one (1) apply to each row.
levelIf MultiIndex, specify the conversion level.
copyIf True, this parameter makes a copy.

A traveler flies from Detroit, Michigan. They are on vacation for a week’s stay in three (3) cities near Berlin.

What is the time difference from their current location to back home in each city visited?

Run this code to find out!

df = pd.DataFrame({'Dest':  ['Berlin', 'Strausberg', 'Bernau'],
                   'Days':  [7, 7, 7]})
  
index_ = pd.date_range('2021-10-09 10:00', periods=3, freq='W', tz='America/Detroit')
df.index = index_
print(df)
print(df.index)

df = df.tz_convert(tz = 'Europe/Berlin')
print(df)
print(df.index)
  • Line [1] creates a DataFrame from a travel itinerary and saves it to df.
  • Line [2] creates an index based on a start date, three (3) weeks, a frequency ('W'), and a time zone.
  • Line [3] sets index_ as the index for the DataFrame.
  • Line [4] outputs the DataFrame to the terminal.
  • Line [5] outputs the index of the DataFrame to the terminal.
  • Line [6] converts the time zone from 'America/Detroit' to 'Europe/Berlin'.
  • Line [7] outputs the updated DataFrame to the terminal.
  • Line [8] outputs the updated index to the terminal.

OutputBefore tz_convert

df

 DestDays
2021-10-10 10:00:00-04:00Berlin    7
2021-10-17 10:00:00-04:00 Strausberg    7
2021-10-24 10:00:00-04:00     Bernau    7

df-index

DatetimeIndex(['2021-10-10 10:00:00-04:00', 
               '2021-10-17 10:00:00-04:00',
               '2021-10-24 10:00:00-04:00'],
               dtype='datetime64[ns, America/Detroit]', freq='W-SUN')

Output – After tz_convert

df

 DestDays
2021-10-10 16:00:00+02:00Berlin    7
2021-10-17 16:00:00+02:00 Strausberg    7
2021-10-24 16:00:00+02:00     Bernau    7

df-index

DatetimeIndex(['2021-10-10 16:00:00+02:00', 
              '2021-10-17 16:00:00+02:00',
              '2021-10-24 16:00:00+02:00'],
              dtype='datetime64[ns, Europe/Berlin]', freq='W-SUN')

Note: Definition of frequency period [W-SUN]:

  • W: weekly
  • SUN: each week starts on a Sunday

More Pandas DataFrame Methods

Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:

Also, check out the full cheat sheet overview of all Pandas DataFrame methods.