
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Create a PeriodIndex and Set Frequency in Python Pandas
To create a PeriodIndex, use the pandas.PeriodIndex() method. To set the frequency, use the freq parameter.
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(['2021-09-25', '2020-10-30', '2020-11-20'], freq="D")
Display PeriodIndex object −
print("PeriodIndex...\n", periodIndex)
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(['2021-09-25', '2020-10-30', '2020-11-20'], freq="D") # Display PeriodIndex object print("PeriodIndex...\n", periodIndex) # Display day from the PeriodIndex object print("\nThe number of days from the PeriodIndex...\n", periodIndex.day)
Output
This will produce the following code −
PeriodIndex... PeriodIndex(['2021-09-25', '2020-10-30', '2020-11-20'], dtype='period[D]') The number of days from the PeriodIndex... Int64Index([25, 30, 20], dtype='int64')
Advertisements