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 Python skills with w3resource's quiz



Python: Tips of the Day

Merging two dicts in Python 3.5+ with a single expression

Example:

# How to merge two dictionaries
# in Python 3.5+

x = {'p': 1, 'q': 3}
y = {'q': 5, 'r': 8}

z = {**x, **y}

z
{'r': 4, 'p': 1, 'q': 3}

z = dict(x, **y)
print(z)

Output:

{'p': 1, 'q': 5, 'r': 8}