Python | Pandas Period.days_in_month

Python Methods and Functions

Period.days_in_month Pandas Period.days_in_month returns the number of days in a month present in this Period object.

Syntax: Period.days_in_month

Parameters: None

Return : number of days

Example # 1: Use the Period.days_in_month attribute to find the number of days present in a month in this Period object.

# import pandas as pd

import pandas as pd

 
# Create Period Object

prd = pd. Period (freq = ' D' , year = 2001 , month = 2 , day = 21 )

 
# Print Period Object

print (prd)

Output:

We will now use the Period.days_in_month attribute to find the number of days present in the month represented by the given period object.

# return the number of days
prd.days_in_month

Output:

As we can see in the output, the Period.days_in_month attribute returned 28, indicating that the month represented in this period object is 28 days.

Example # 2: Use the Period.days_in_month attribute to find the number of days present in a month in a given Period object.

# import pandas as pd

import pandas as pd

 
# Create Period Object

prd = pd.Period (freq = 'Q' , year = 2006 , quarter = 1 )

 
# Print the object

print (prd)

Output:

Now we will use the Period.days_in_month to find the number of days present in the month represented by this period object.

# return the number of days
prd.days_in_month

Output:

As we can see in the output, the attribute Period.days_in_month returned 31, indicating that the month represented by this period object has 31 days.





Get Solution for free from DataCamp guru