

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Pandas - Create a PeriodIndex and get the day of the month
To create a PeriodIndex, use the pandas.PeriodIndex() method. Get the days of the month using the PeriodIndex.daysinmonth property
At first, import the required libraries −
import pandas as pd
Create a PeriodIndex object. PeriodIndex is an immutable ndarray holding ordinal values indicating regular periods in time. We have set the frequency using the "freq" parameter −
periodIndex = pd.PeriodIndex(['2018-07-25', '2019-10-30', '2020-11-20', '2021-09-15', '2022-03-12', '2023-06-18'], freq="D")
Display PeriodIndex object −
print("PeriodIndex...\n", periodIndex)
Display days in the specific month from the PeriodIndex object −
print("\nDays of the specific month from the PeriodIndex...\n", periodIndex.daysinmonth)
Example
Following is the code −
import pandas as pd # Create a PeriodIndex object # PeriodIndex is an immutable ndarray holding ordinal values indicating regular periods in time # We have set the frequency using the "freq" parameter periodIndex = pd.PeriodIndex(['2018-07-25', '2019-10-30', '2020-11-20', '2021-09-15', '2022-03-12', '2023-06-18'], freq="D") # Display PeriodIndex object print("PeriodIndex...\n", periodIndex) # Display PeriodIndex frequency print("\nPeriodIndex frequency...\n", periodIndex.freq) # Display the month number i.e. 1 = January, 2 = February ... 12 = December print("\nMonth number...\n", periodIndex.month) # Display days in the specific month from the PeriodIndex object print("\nDays of the specific month from the PeriodIndex...\n", periodIndex.daysinmonth)
Output
This will produce the following code −
PeriodIndex... PeriodIndex(['2018-07-25', '2019-10-30', '2020-11-20', '2021-09-15', '2022-03-12', '2023-06-18'], dtype='period[D]') PeriodIndex frequency... <Day> Month number... Int64Index([7, 10, 11, 9, 3, 6], dtype='int64') Days of the specific month from the PeriodIndex... Int64Index([31, 31, 30, 30, 31, 30], dtype='int64')
- Related Questions & Answers
- Python Pandas - Create a PeriodIndex and get the day of the year
- Python Pandas - Create a PeriodIndex and get the days of the period
- Python Pandas - Create a PeriodIndex and get the days of the week
- Python Pandas - Get the day of the week from the PeriodIndex object
- Python Pandas - Get the month number of the period from the PeriodIndex object
- Python Pandas - Get day of the month that a Period falls on
- Python Pandas - Create a PeriodIndex and set frequency
- Python Pandas - Get the year from the PeriodIndex object
- Python Pandas - Get the hour of the period from the PeriodIndex object
- Python Pandas - Get the minute of the period from the PeriodIndex object
- Python Pandas - Get the seconds of the period from the PeriodIndex object
- Python Pandas - Get the week of the period from the PeriodIndex object
- Get the last day of month in Java
- Python Pandas - Get the month of the year component of the Period
- How to get the day of the month in JavaScript?
Advertisements