How to return the number of days in a month in pandas

Overview

The .daysinmonth attribute of the Timestamp object (an equivalent of Python's datetime object) is used to obtain the number of days present in a month.

Syntax

Timestamp.daysinmonth
Syntax for the .daysinmonth attribute in Pandas

Parameters

This attribute takes no parameter value.

Return value

It returns an integer representing the number of days in the given month.

Example

# A code to illustrate the .daysinmonth attribute in Pandas
# importing the pandas library
import pandas as pd
# creating a Timestamp object
a = pd.Timestamp(2022, 4, 19)
# obtaining the number of days present in the month 4 (i.e April)
print(a.daysinmonth)

Explanation

  • Line 3: We import the pandas module.
  • Line 6: We create a Timestamp object, a.
  • Line 9: We obtain the number of days present in the given month, 4 , in a using the .daysinmonth attribute.

Free Resources