Timetuple – Python Pandas Timestamp.timetuple() Function

What is Timestamp?

Timetuple: 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:

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

The Timestamp.timetuple() function of the Pandas module returns a time tuple for the specified Timestamp object. The returned tuple comprises values ranging from the year, month to hours, and seconds. The time tuple is equivalent to time.localtime().

Syntax:

Timestamp.timetuple()

Parameters: It has no arguments

Return Value:

The time tuple comprises values ranging from the year, month to hours, and seconds is returned by the Timestamp.timetuple() function

Pandas Timestamp.timetuple() Function in Python

Example1

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
  • Store it in a variable
  • Print the above-obtained Timestamp object
  • Apply timetuple() function on the above Timestamp object to get the time tuple for the given timestamp object.
  • Here it returns a tuple comprised of values ranging from the year, month to hour, and seconds.
  • 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
# Store it in a variable
time_stamp_obj = pd.Timestamp(year = 2017,  month = 6, day = 10, hour = 5, 
                            second = 22, tz = 'Asia/Kolkata')
  
# Print the above obtained Timestamp object
print("The above obtained Timestamp object:", time_stamp_obj)
# Apply timetuple() function on the above Timestamp object to 
# get the time tuple for the given timestamp object.
# Here it returns a tuple comprises of values ranging from the year, month to hour and seconds.
print("The time tuple for the given timestamp object:")
time_stamp_obj.timetuple()

Output:

The above obtained Timestamp object: 2017-06-10 05:00:22+05:30
The time tuple for the given timestamp object:
time.struct_time(tm_year=2017, tm_mon=6, tm_mday=10, tm_hour=5, tm_min=0, tm_sec=22, tm_wday=5,
tm_yday=161, tm_isdst=0)

Example2

Approach:

  • Import pandas module using the import keyword.
  • Pass some random year, month, day, hour, second, tz =’US/Central'(Timezone) as the arguments to the Timestamp() function of the pandas module to get the Timestamp object.
  • Store it in a variable
  • Print the above-obtained Timestamp object
  • Apply timetuple() function on the above Timestamp object to get the time tuple for the given timestamp object.
  • Here it returns a tuple comprised of values ranging from the year, month to hour, and seconds.
  • 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/Central'
# (Timezone) as the arguments to the Timestamp() function of the
# pandas module to get the Timestamp object
# Store it in a variable
time_stamp_obj = pd.Timestamp(year = 2021,  month = 1, day = 5, hour = 10, 
                            second = 25, tz = 'US/Central')
  
# Print the above obtained Timestamp object
print("The above obtained Timestamp object:", time_stamp_obj)
# Apply timetuple() function on the above Timestamp object to 
# get the time tuple for the given timestamp object.
# Here it returns a tuple comprises values ranging from the year, month to hour and seconds.
print("The time tuple for the given timestamp object:")
time_stamp_obj.timetuple()

Output:

The above obtained Timestamp object: 2021-01-05 10:00:25-06:00
The time tuple for the given timestamp object:
time.struct_time(tm_year=2021, tm_mon=1, tm_mday=5, tm_hour=10, tm_min=0, tm_sec=25, tm_wday=1,
tm_yday=5, tm_isdst=0)