How to get the ISO year, week number, and weekday in pandas

Overview

The isocalendar() function in Pandas returns the ISO year, week number, and weekday from a Timestamp object (an equivalent of Python's datetime object).

Syntax

The isocalendar() function takes the following syntax:

Timestamp.isocalendar()
The syntax for the isocalendar() function in pandas

Parameter value

The isocalendar() function takes no parameter value.

Return value

The isocalendar() function returns a 3-tuple holding the results.

Example

# A code to illustrate the isocalendar() function in Pandas
# importing the pandas library
import pandas as pd
# creating a date time object
my_date = pd.Timestamp('2022-04-29T7:48:52.192548651')
# obtaining the iso year, week number, and weeekday from the Timestamp
print(my_date.isocalendar())

Explanation

  • Line 3: We import the pandas module.
  • Line 6: We create a Timestamp (datetime) object and name it my_date.
  • Line 9: We obtain the ISO year, week number, and weekday from the given datetime object, my_date, using the isocalendar() function.

Free Resources