How to obtain the total number of days in a month

Overview

To obtain the number of days in a month of a Period object, we use the daysinmonth attribute.

Syntax

The daysinmonth attribute takes the syntax below:

Period.daysinmonth
The syntax for the daysinmonth attribute in pandas

Parameter value

Since it is an attribute, daysinmonth takes no parameter value.

Return value

daysinmonth returns an int representing the number of days in a given month of a Period object.

Example

# A code to illustrate the daysinmonth attribute
# importing the pandas library
import pandas as pd
# creating a Period object
a = pd.Period('2022-5-2 8:00', 'H')
# obtaining the number of days in the month
b = a.daysinmonth
# printing the value
print("There are", b, "days in the given month")

Explanation

  • Line 3: We import the pandas library.
  • Line 6: We create a Period object, a.
  • Line 9: We obtain the day of the month from the Period object using the daysinmonth attribute. We assign the result to a variable, b.
  • Line 12: We print the value of b to the console.