Python Pandas Timestamp.fromordinal() Function

What is Timestamp?

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.fromordinal() Function:

When an ordinal is provided to the Timestamp.fromordinal() function of the pandas module, it returns a Timestamp object after translating and converting it to a ts object. There can’t be any tz information(tzinfo) on the ordinal itself, by definition.

Syntax:

 Timestamp.fromordinal()

Parameters: 

ordinal: It is the date corresponding to a proleptic Gregorian ordinal

freq: This is optional. It is the offset that a Timestamp has.

tz: This is the timezone given as input.

Return Value:

The Timestamp object is returned by the Timestamp.fromordinal() function of the pandas module.

Pandas Timestamp.fromordinal() Function in Python

Example1

Here, the function returns a new Timestamp object based on the given ordinal value.

Approach:

  • Import pandas module using the import keyword.
  • Pass some random year, month, day, hour, second, 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 ordinal value as an argument to the fromordinal() function and apply it on the above Timestamp object to convert the above-given Timestamp object according to the ordinal(numerical) value.
  • 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, second, 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 = 2019,  month = 5, day = 16, hour = 11, 
                            second = 25, tz = 'Asia/Kolkata')
  
# Print the above obtained Timestamp object
print("The above obtained Timestamp object:", time_stamp_obj)
print()
# Pass some random ordinal value as an argument to the fromordinal() function 
# and apply it on the above Timestamp object to convert the 
# above given Timestamp object according the ordinal(numerical) value.
print("The given Timestamp object according to the ordinal value:")
time_stamp_obj.fromordinal(ordinal = 723826)

Output:

The above obtained Timestamp object: 2019-05-16 11:00:25+05:30

The given Timestamp object according to the ordinal value:
Timestamp('1982-10-08 00:00:00')

Example2

Here, it converts the Timestamp object according to the ordinal(numerical) value and sets the given timezone for the Timestamp.

Approach:

  • Import pandas module using the import keyword.
  • Pass some random year, month, day, hour, second, tz = ‘US/Eastern’ (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 ordinal value, tz as the arguments to the fromordinal() function and apply it on the above Timestamp object to convert the above-given Timestamp object according to the ordinal(numerical) value and set the given timezone for the Timestamp.
  • 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, second, tz = 'US/Eastern'
# (Timezone) as the arguments to the Timestamp() function of the
# pandas module to get the Timestamp object
time_stamp_obj = pd.Timestamp(year = 2020,  month = 3, day = 12, hour = 13, 
                            second = 30, tz = 'US/Eastern')
  
# Print the above obtained Timestamp object
print("The above obtained Timestamp object:", time_stamp_obj)
print()
# Pass some random ordinal value, tz as the arguments to the fromordinal() function 
# and apply it on the above Timestamp object to convert the above given Timestamp 
# object according to the ordinal(numerical) value and set the given timezone for the Timestamp
print("The Timestamp object according to the ordinal and timezone Values:")
time_stamp_obj.fromordinal(ordinal = 693745, tz= 'Asia/Kolkata')

Output:

The above obtained Timestamp object: 2020-03-12 13:00:30-04:00

The Timestamp object according to the ordinal and timezone Values:
Timestamp('1900-05-30 00:00:00+0553', tz='Asia/Kolkata')

Leave a Comment