How to return the day of the year in pandas
Overview
To obtain the date of the year from the Pandas' Timestamp object, which is an equivalent of Python's datetime object, we make use of the .dayofyear attribute.
Syntax
Timestamp.dayofyear
Syntax for the .day_of_year attribute in Pandas
Parameters
This attribute does not take any parameter value.
Return value
It returns an integer representing the day of the year.
Example
# A code to illustrate the .daysofyear attribute in Pandas# importing the pandas libraryimport pandas as pd# creating a Timestamp objecta = pd.Timestamp(2020, 4, 19)# getting the day of the yearprint(a.dayofyear)
Explanation
- Line 3: We import the
pandaslibrary. - Line 6: We create a Timestamp object,
a. - Line 9: We obtain the day of the year from
ausing the.dayofyearattribute.