Creating a Timestamp object with different time zones in pandas
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 libraryimport pandas as pd# creating a Timestamp object with a UTC time zonea = pd.Timestamp('2020-03-14T15:32:52.192548651', tz="UTC")# converting the timestamp object with UTC to Abidjan timezoneAbidjan_time = a.astimezone('Africa/Abidjan')print(Abidjan_time)
Explanation
- Line 2: We import the
pandasmodule. - Line 5: We create a
Timestampobject 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.