Python Pandas Timestamp.week Attribute

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.week Attribute:

The Timestamp.week attribute of the Pandas module returns an integer value representing the ordinal(numerical) value of the week in which the given Timestamp object’s date falls.

Syntax:

 Timestamp.week

Parameters: It has no arguments

Return Value:

The week value in which the given Timestamp object’s date falls is returned by the Timestamp.week attribute of the Pandas module.

Pandas Timestamp.week Attribute in Python

Example1

Here, the Timestamp.week attribute returns 24, representing the date in the given Timestamp object falls in the 24th week of the year.

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
  • Apply the week attribute to the above Timestamp object to get the week in which the date in the above-given Timestamp object falls.
  • Here it is the 24th week of the year.
  • 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 = 6, day = 15, hour = 5, 
                            minute = 12, tz = 'Asia/Kolkata')
  
# Print the above obtained Timestamp object
print("The above obtained Timestamp object:", time_stamp_obj)
# Apply week attribute to the above Timestamp object to get the 
# week in which the date in the above given Timestamp object falls
# Here it is the 24th week of the year.
print("The week in which the date in the above given Timestamp object falls:")
time_stamp_obj.week

Output:

The above obtained Timestamp object: 2021-06-15 05:12:00+05:30
The week in which the date in the above given Timestamp object falls:
24

Example2

Here it is the 35th week of the year. We changed the timezone to ‘Brazil/East’ here.

Approach:

  • Import pandas module using the import keyword.
  • Pass some random year, month, day, hour, minute, tz =‘Brazil/East’(Timezone) as the arguments to the Timestamp() function of the pandas module to get the Timestamp object
  • Print the above-obtained Timestamp object
  • Apply the week attribute to the above Timestamp object to get the week in which the date in the above-given Timestamp object falls.
  • Here it is the 35th week of the year.
  • 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 ='Brazil/East'
# (Timezone) as the arguments to the Timestamp() function of the
# pandas module to get the Timestamp object
time_stamp_obj = pd.Timestamp(year = 2018,  month = 8, day = 28, hour = 9, 
                            minute = 30, tz = 'Brazil/East')
  
# Print the above obtained Timestamp object
print("The above obtained Timestamp object:", time_stamp_obj)
# Apply week attribute to the above Timestamp object to get the 
# week in which the date in the above given Timestamp object falls
# Here it is the 35th week of the year.
print("The week in which the date in the above given Timestamp object falls:")
time_stamp_obj.week

Output:

The above obtained Timestamp object: 2018-08-28 09:30:00-03:00
The week in which the date in the above given Timestamp object falls:
35