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 library
import pandas as pd
# creating a Timestamp object
a = pd.Timestamp(2020, 4, 19)
# getting the day of the year
print(a.dayofyear)

Explanation

  • Line 3: We import the pandas library.
  • Line 6: We create a Timestamp object, a.
  • Line 9: We obtain the day of the year from a using the .dayofyear attribute.

Free Resources