Pandas Series dt.tz_convert | Change Timezone in DateTime Series
Last Updated :
08 Feb, 2024
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
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
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
result = sr.dt.tz_convert(tz = 'Asia / Calcutta' )
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...