What is Timestamp?
Astimezone: A timestamp is a sequence of characters or encoded information that identifies when a particular event occurred, typically providing the date and time of day, and can be accurate to a fraction of a second.
The timestamp method is used for a variety of synchronization purposes, including assigning a sequence order to a multievent transaction so that the transaction can be canceled if a fault occurs. A timestamp can also be used to record time in reference to a specific starting point in time.
Uses of Timestamp:
Timestamps are used to maintain track of information stored online or on a computer. A timestamp indicates when data was generated, shared, modified, or removed.
Here are some examples of how timestamps can be used:
- A timestamp in a computer file indicates when the file was last modified.
- Photographs with digital cameras have timestamps that show the date and time of day they were taken.
- The date and time of the post are included in social media posts.
- Timestamps are used in online chat and instant messages to record the date and time that a message was delivered, received, or viewed.
- Timestamps are used in blockchain blocks to confirm the validity of transactions, such as those involving cryptocurrencies.
- To secure the integrity and quality of data, data management relies on timestamps.
- Timestamps are used in digital contracts and digital signatures to signify when a document was signed.
Pandas Timestamp.astimezone Function:
The Timestamp.astimezone() function of the Pandas module converts a tz-aware Timestamp to a different(other) time zone. The function returns an error for tz-naive Timestamp objects.
Syntax:
Timestamp.astimezone(tz)
Parameters:
tz: This is required. It is the Timezone that is to be converted.
Return Value:
The converted/changed Timestamp value is returned by the Timestamp.astimezone() function of the Pandas module
Pandas Timestamp.astimezone Function in Python
Example1
Here it changes the timezone from ‘Asia/Kolkata’ to ‘Brazil/East’
Approach:
- Import pandas module using the import keyword.
- Pass some random year, month, day, hour, minute, tz =’Asia/Kolkata’ (Timezone) as the arguments to the Timestamp() function of the pandas module to get the Timestamp object
- Print the above-obtained Timestamp object.
- Pass some random timezone as an argument to the astimezone() function and apply it to the given Timestamp object to convert to the given Timestamp.
- Here it changes the timezone from ‘Asia/Kolkata’ to ‘Brazil/East’.
- The Exit of the Program.
Below is the implementation:
# Import pandas module using the import keyword. import pandas as pd # Pass some random year, month, day, hour, minute, tz ='Asia/Kolkata' # (Timezone) as the arguments to the Timestamp() function of the # pandas module to get the Timestamp object time_stamp_obj = pd.Timestamp(year = 2015, month = 6, day = 26, hour = 7, minute = 12, tz = 'Asia/Kolkata') # Print the above obtained Timestamp object print("The above obtained Timestamp object:", time_stamp_obj) # Pass some random timezone as an argument to the astimezone() function and # apply it to the given Timestamp object to convert to the given Timestamp. # Here it changes the timezone from 'Asia/Kolkata' to 'Brazil/East' print("Changing timezone from 'Asia/Kolkata' to 'Brazil/East':") time_stamp_obj.astimezone('Brazil/East')
Output:
The above obtained Timestamp object: 2015-06-26 07:12:00+05:30 Changing timezone from 'Asia/Kolkata' to 'Brazil/East': Timestamp('2015-06-25 22:42:00-0300', tz='Brazil/East')
Example2
Here it changes the timezone from ‘Asia/Kolkata’ to ‘US/Central’
Approach:
- Import pandas module using the import keyword.
- Pass some random year, month, day, hour, minute, tz =’Asia/Kolkata’ (Timezone) as the arguments to the Timestamp() function of the pandas module to get the Timestamp object
- Print the above-obtained Timestamp object.
- Pass some random timezone as an argument to the astimezone() function and apply it to the given Timestamp object to convert to the given Timestamp.
- Here it changes the timezone from ‘Asia/Kolkata’ to ‘US/Central’.
- The Exit of the Program.
Below is the implementation:
# Import pandas module using the import keyword. import pandas as pd # Pass some random year, month, day, hour, minute, tz ='Asia/Kolkata' # (Timezone) as the arguments to the Timestamp() function of the # pandas module to get the Timestamp object time_stamp_obj = pd.Timestamp(year = 2021, month = 5, day = 16, hour = 12, minute = 28, tz = 'Asia/Kolkata') # Print the above obtained Timestamp object print("The above obtained Timestamp object:", time_stamp_obj) # Pass some random timezone as an argument to the astimezone() function and # apply it to the given Timestamp object to convert to the given Timestamp. # Here it changes the timezone from 'Asia/Kolkata' to 'US/Central' print("Changing timezone from 'Asia/Kolkata' to 'US/Central':") time_stamp_obj.astimezone('US/Central')
Output:
The above obtained Timestamp object: 2021-05-16 12:28:00+05:30 Changing timezone from 'Asia/Kolkata' to 'US/Central': Timestamp('2021-05-16 01:58:00-0500', tz='US/Central')