+

Python | Pandas Series.tz_convert

Pandas Series — it is a one-dimensional array with axis labels. Tags do not have to be unique, but must be hashable. The object supports indexing based on integers and labels and provides many methods for performing index-related operations.

Series.tz_convert() Pandas Series.tz_convert () works with indexes that support the time zone. It converts the tz-aware axis to the target timezone.

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

Parameter:
tz: string or pytz.timezone object
axis: the axis to convert
level: int, str, default None
copy: Also make a copy of the underlying data.

Returns: Series

Example # 1: Use Series.tz_convert () to convert the Sentinel Awareness Index the belt of the given series in the target time zone.

# import pandas as pd

import pandas as pd

 
# Create a series

sr = pd.Series ([ `New York` , ` Chicago` , `Toronto` , ` Lisbon` , `Rio` , ` Moscow` ])

 
# Create date and time index

didx = pd.DatetimeIndex (start = `2014-08-01 10:00` , freq = ` W`

p eriods = 6 , tz = ` Asia / Calcutta`

  
# set index

sr.index = didx

 
# Print series

print (sr)

Output:

We will now use Series.tz_convert () to convert the given timezone index to an index from Series.tz_convert () the time zone to the target time zone, which is US / Central.

 

# convert to & # 39; US / Central & # 39;

sr.tz_convert ( `US / Central` )

Output:

As we can see in the output, Series.tz_convert () converted the index time zone of this series object to the correct time zone.

Example # 2: Use Series.tz_convert () to convert the time zone awareness index of the given series to the target time zone.

# import pandas as pd

import pandas as pd

< p>  
# Create series

sr = pd.Series ([ 19.5 , 16.8 , 22.78 , 20.124 , 18.1002 ])

 
# Create an index date and time

didx = pd. DatetimeIndex (start = `2014-08-01 10:00` , freq = `W`

periods = 5 , tz = `Asia / Calcutta`

 
# set index

sr. index = didx

  
# Print series

print (sr )

Output:

We will now use Series.tz_convert () to converting the given time zone index to an index from the Series.tz_convert () time zone to a target time zone called Europe / Berlin. < / p>

# convert to & # 39; Europe / Berlin & # 39;

sr .tz_convert ( `Europe / Berlin` )

Output:

Get Solution for free from DataCamp guru