Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

pandas
data science
communitycreator
timestamp

How to return the day of the year in pandas

Onyejiaku Theophilus Chidalu

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)
Implementation of the .dayofyear attribute

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.

RELATED TAGS

pandas
data science
communitycreator
timestamp

CONTRIBUTOR

Onyejiaku Theophilus Chidalu
RELATED COURSES

View all Courses

Keep Exploring