What is the Period.dayofyear attribute in Pandas?

Overview

In Pandas, the dayofyear() attribute is used to obtain the day of the year on which the given Period occurs. The return value from this attribute ranges from 1 to 365 for a typical year and from 1 to 366 for leap years.

Syntax

The dayofyear attribute of a Period has the following syntax:

Period.dayofyear
Syntax for the dayofyear attribute in Pandas

Parameter value

dayofyear, being an attribute, takes no parameter value.

Return value

The dayofyear attribute returns an int, which represents the day of the year from the given Period object.

Example

Let’s look at the code example given below:

# A code to illustrate the dayofyear attribute
# importing the pandas library
import pandas as pd
# creating a Period object
a = pd.Period('2022-5-2 8:00', 'H')
# obtaining the day of the year
b = a.dayofyear
# printing the value
print("Today is day", b, "of the year 2022")

Explanation

  • Line 3: We import the pandas library.
  • Line 6: We create a Period object called a.
  • Line 9: We use the dayofyear attribute to obtain the day of the year from the Period object. We assign the result to a variable called b.
  • Line 12: We print the value of b to the console.

Free Resources