Python | Pandas Series.tz_convert
Last Updated :
30 Jan, 2019
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index.
Pandas Series.tz_convert()
function works with time zone aware indexes. It convert tz-aware axis to target time zone.
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()
function to convert the time zone aware index of the given Series to the target time zone.
import pandas as pd
sr = pd.Series([ 'New York' , 'Chicago' , 'Toronto' , 'Lisbon' , 'Rio' , 'Moscow' ])
didx = pd.DatetimeIndex(start = '2014-08-01 10:00' , freq = 'W' ,
periods = 6 , tz = 'Asia/Calcutta' )
sr.index = didx
print (sr)
|
Output :
Now we will use Series.tz_convert()
function to convert the given time zone index to time zone aware index to the target time zone which is ‘US/Central’.
sr.tz_convert( 'US/Central' )
|
Output :
As we can see in the output, the Series.tz_convert()
function has converted the time zone of the index of the given series object to the desired time zone.
Example #2: Use Series.tz_convert()
function to convert the time zone aware index of the given Series to the target time zone.
import pandas as pd
sr = pd.Series([ 19.5 , 16.8 , 22.78 , 20.124 , 18.1002 ])
didx = pd.DatetimeIndex(start = '2014-08-01 10:00' , freq = 'W' ,
periods = 5 , tz = 'Asia/Calcutta' )
sr.index = didx
print (sr)
|
Output :
Now we will use Series.tz_convert()
function to convert the given time zone index to time zone aware index to the target time zone which is ‘Europe/Berlin’
sr.tz_convert( 'Europe/Berlin' )
|
Output :
Share your thoughts in the comments
Please Login to comment...