Pandas Series dt.tz_convert | Change Timezone in DateTime Series
The dt.tz_convert() method converts tz-aware DateTime Series object from one time zone to another.
Example
Python3
import pandas as pd sr = pd.Series(pd.date_range( '2012-12-31 00:00' , periods = 5 , freq = 'D' , tz = 'US / Central' )) idx = [ 'Day 1' , 'Day 2' , 'Day 3' , 'Day 4' , 'Day 5' ] sr.index = idx result = sr.dt.tz_convert(tz = 'Europe / Berlin' ) print (result) |
Output:
Syntax
Syntax: Series.dt.tz_convert(tz)
Parameter
- tz: Time zone to convert timestamps to.
Returns: series of same type as input
How to Convert Timezones For tz-aware DateTime Series
To convert timezones from one timezone to another in a tz-aware DateTime Series we use the dt.tz_convert method of the Pandas library in Python.
Let us understand it better with an example:
Example:
Use the Series.dt.tz_convert() function to convert the timezone of the timestamps in the given series object.
Python3
# importing pandas as pd import pandas as pd # Creating the Series sr = pd.Series(pd.date_range( '2012-12-31 00:00' , periods = 5 , freq = 'D' , tz = 'US / Central' )) # Creating the index idx = [ 'Day 1' , 'Day 2' , 'Day 3' , 'Day 4' , 'Day 5' ] # set the index sr.index = idx # Print the series print (sr) |
Output :
Now we will use the dt.tz_convert() function to convert the timestamps in the given series object to ‘Asia/Calcutta’.
Python3
# convert to 'Asia / Calcutta' result = sr.dt.tz_convert(tz = 'Asia / Calcutta' ) # print the result print (result) |
Output :
As we can see in the output, the dt.tz_convert() function has successfully converted the timezone of the timestamps in the given series object to the target timezone.