w3resource

Pandas: Create a time series object with a time zone

Pandas Time Series: Exercise-18 with Solution

Write a Pandas program to create a time series object with a time zone.

Sample Solution:

Python Code :

import pandas as pd
print("Timezone: Europe/Berlin:")
print("Using pytz:")
date_pytz = pd.Timestamp('2019-01-01', tz = 'Europe/Berlin')
print(date_pytz.tz)  
print("Using dateutil:")
date_util = pd.Timestamp('2019-01-01', tz = 'dateutil/Europe/Berlin')
print(date_util.tz)
print("\nUS/Pacific:")
print("Using pytz:")
date_pytz = pd.Timestamp('2019-01-01', tz = 'US/Pacific')
print(date_pytz.tz)  
print("Using dateutil:")
date_util = pd.Timestamp('2019-01-01', tz = 'dateutil/US/Pacific')
print(date_util.tz)

Sample Output:

Timezone: Europe/Berlin:
Using pytz:
Europe/Berlin
Using dateutil:
tzfile('/usr/share/zoneinfo/Europe/Berlin')

US/Pacific:
Using pytz:
US/Pacific
Using dateutil:
tzfile('/usr/share/zoneinfo/US/Pacific')

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Pandas program to convert unix/epoch time to a regular time stamp in UTC. Also convert the said timestamp in to a given time zone.
Next:Write a Pandas program to remove the time zone information from a Time series data.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.