Thursday, December 26, 2024
Google search engine
HomeLanguagesPython | Pandas DatetimeIndex.tz_convert()

Python | Pandas DatetimeIndex.tz_convert()

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 DatetimeIndex.tz_convert() function convert tz-aware DatetimeIndex from one time zone to another. The function takes an input parameter which is the time zone to which we want to convert the current DatetimeIndex object to.

Syntax: DatetimeIndex.tz_convert(tz)

Parameters :
tz : Time zone for time. Corresponding timestamps would be converted to this time zone of the DatetimeIndex. A tz of None will convert to UTC and remove the timezone information.

Return : normalized : DatetimeIndex

Example #1: Use DatetimeIndex.tz_convert() function to convert the given DatetimeIndex object to the desired time zone.




# importing pandas as pd
import pandas as pd
  
# Create the DatetimeIndex
# Here 'Q' represents quarter end frequency 
didx = pd.DatetimeIndex(start ='2000-01-15 08:00', freq ='Q',
                            periods = 4, tz ='Asia/Calcutta')
  
# Print the DatetimeIndex
print(didx)


Output :

Now we want to convert the timezone from ‘Asia/Calcutta’ to ‘US/Central’




# Convert the timezone to 'US / Central'
didx.tz_convert('US/Central')</div>


Output :

As we can see in the output, the function has changed the timezone of the didx object.
 
Example #2: Use DatetimeIndex.tz_convert() function to convert the given DatetimeIndex object to the desired time zone.




# importing pandas as pd
import pandas as pd
  
# Create the DatetimeIndex
# Here 'Q' represents quarter end frequency 
didx = pd.DatetimeIndex(start ='2000-01-15 08:00', freq ='Q'
                           periods = 4, tz ='Asia/Calcutta')
  
# Print the DatetimeIndex
print(didx)


Output :

Now we want to convert the timezone from ‘Asia/Calcutta’ to ‘Europe/Berlin’




# Convert the timezone to 'Europe / Berlin'
didx.tz_convert('Europe/Berlin')


Output :

As we can see in the output, the function has changed the timezone of the didx object.

Last Updated :
24 Dec, 2018
Like Article
Save Article

<!–

–>

Similar Reads
Related Tutorials
Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments