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 libraryimport pandas as pd# creating a Period objecta = pd.Period('2022-5-2 8:00', 'H')# obtaining the number of days in the monthb = a.daysinmonth# printing the valueprint("There are", b, "days in the given month")
Explanation
- Line 3: We import the pandas library.
- Line 6: We create a
Periodobject,a. - Line 9: We obtain the day of the month from the
Periodobject using thedaysinmonthattribute. We assign the result to a variable,b. - Line 12: We print the value of
bto the console.