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 libraryimport pandas as pd# creating a Period objecta = pd.Period('2022-5-2 8:00', 'H')# obtaining the day of the yearb = a.dayofyear# printing the valueprint("Today is day", b, "of the year 2022")
Explanation
- Line 3: We import the
pandaslibrary. - Line 6: We create a
Periodobject calleda. - Line 9: We use the
dayofyearattribute to obtain the day of the year from thePeriodobject. We assign the result to a variable calledb. - Line 12: We print the value of
bto the console.