Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

pandas
data science
timestamp
communitycreator

Creating a Timestamp object with different time zones in pandas

Onyejiaku Theophilus Chidalu

Overview

The astimezone() function of Pandas.Timestamp (an equivalent of Python's datetime object) is used to create Timestamp objects with different time zones.

Syntax

The astimezone() function takes the syntax below:

Timestamp.astimezone(tz)
Syntax for the astimezone() function in Pandas

Parameter value

The astimezone() function takes a single parameter value, tz, representing the time zone to which the Timestamp object will be converted.

Return value

The astimezone() function returns a converted Timestamp object.

Example

We'll convert a UTC time zone to an Abidjan time zone in the example below.

# importing the pandas library
import pandas as pd

# creating a Timestamp object with a UTC time zone
a = pd.Timestamp('2020-03-14T15:32:52.192548651', tz="UTC")

# converting the timestamp object with UTC to Abidjan timezone
Abidjan_time = a.astimezone('Africa/Abidjan')

print(Abidjan_time)

Explanation

  • Line 2: We import the pandas module.
  • Line 5: We create a Timestamp object with a UTC time zone.
  • Line 8: We convert the initial time zone to that of Abidjan using the astimezone() function. We assign the result to a variable, Abidjan_time.
  • Line 10: We print the value of the variable Abidjan_time.

RELATED TAGS

pandas
data science
timestamp
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu
RELATED COURSES

View all Courses

Keep Exploring