Python Pandas Period.week Attribute

Pandas Period.week Attribute:

The Period.week of Pandas module produces an integer value that indicates the week number in which the specified period object lies.

Syntax:

 Period.week

Parameters: It has no arguments.

Return Value:

The week number of the year is returned by the Period.week of Pandas module.

Pandas Period.week Attribute in Python

Example1

Here, the Period.week attribute returns 20, showing that the following period object is in the 20th week of the given year.

Approach:

  • Import pandas module using the import keyword.
  • Pass some random frequency(here S= secondly frequency), year, month, day, hour, minute, second as the arguments to the Period() function of the pandas module to get the Period object
  • Print the given period object
  • Apply the week attribute on the above period object to get the week number of the year in which the given period object lies.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random frequency(here S= secondly frequency), year, month, day,
# hour, minute, second as the arguments to the Period() function
# of the pandas module to get the Period object
period_obj = pd.Period(freq ='S', year = 2015, month = 5, day = 16,
                         hour = 12, minute = 28, second = 10)
  
# Print the given period object
print("The given period object:")
print(period_obj)
print()
# Apply week attribute on the above period object to get the 
# week number of the year in which the given period object lies.
print("The week of the year in which the given period object lies:")
period_obj.week

Output:

The given period object:
2015-05-16 12:28:10

The week of the year in which the given period object lies:
20

Example2

Here, the Period.week attribute returns 35, showing that the following period object is in the 35th week of the given year.

Approach:

  • Import pandas module using the import keyword.
  • Pass some random frequency(here S= secondly frequency), year, month, day, hour, minute, second as the arguments to the Period() function of the pandas module to get the Period object
  • Print the given period object
  • Apply the week attribute on the above period object to get the week number of the year in which the given period object lies.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random frequency(here S= secondly frequency), year, month, day,
# hour, minute, second as the arguments to the Period() function
# of the pandas module to get the Period object
period_obj = pd.Period(freq ='S', year = 2020, month = 8, day = 30,
                         hour = 10, minute = 25, second = 22)
  
# Print the given period object
print("The given period object:")
print(period_obj)
print()
# Apply week attribute on the above period object to get the 
# week number of the year in which the given period object lies.
print("The week of the year in which the given period object lies:")
period_obj.week

Output:

The given period object:
2020-08-30 10:25:22

The week of the year in which the given period object lies:
35