Python Pandas Period.dayofyear Attribute

Pandas Period.dayofyear Attribute:

The Period.dayofyear attribute of the Pandas module gives the day of the year for the specified Period object. This attribute returns an integer value.

Syntax:

Period.dayofyear

Parameters: It has no arguments

Return Value:

The day of the year for the given period object is returned by the Period.dayofyear attribute of the Pandas module.

Pandas Period.dayofyear Attribute in Python

Example1

Here, the Period.dayofyear attribute returns 181, showing that the day was the 181 day of the year.

Approach:

  • Import pandas module using the import keyword.
  • Pass some random frequency(here Q=quaterly), year, quarter as the arguments to the Period() function
    of the pandas module to get the Period object
  • Store it in a variable
  • Print the given period object
  • Apply dayofyear attribute on the above period object to get the day of the year in the above given period object.
  • 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 Q=quaterly), year, quarter
# as the arguments to the Period() function
# of the pandas module to get the Period object
# Store it in a variable
period_obj = pd.Period(freq ='Q', year = 2021, quarter =2)
  
# Print the given period object
print("The given period object:")
print(period_obj)
print()
# Apply dayofyear attribute on the above period object to get the 
# day of the year in the above given period object
print("The day of the year in the above given period object:")
period_obj.dayofyear

Output:

The given period object:
2021Q2

The day of the year in the above given period object:
181

Example2

Here, the Period.dayofyear attribute returns 101, showing that the day was the 101 day of the year.

Approach:

  • Import pandas module using the import keyword.
  • Pass some random frequency(here D= Daily frequency), year, month, day as the arguments to the Period() function of the pandas module to get the Period object
  • Store it in a variable
  • Print the given period object
  • Apply dayofyear attribute on the above period object to get the day of the year in the above given period object.
  • 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 D= Daily frequency), year, month, day as the arguments 
# to the Period() function of the pandas module to get the Period object
# Store it in a variable
period_obj = pd.Period(freq ='D', year = 2018, month = 4, day = 11)
  
# Print the given period object
print("The given period object:")
print(period_obj)
print()
# Apply dayofyear attribute on the above period object to get the 
# day of the year in the above given period object
print("The day of the year in the above given period object:")
period_obj.dayofyear

Output:

The given period object:
2018-04-11

The day of the year in the above given period object:
101