
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Pandas - Create a PeriodIndex and set frequency
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')
- Related Articles
- Python Pandas - Convert PeriodIndex object to Timestamp and set the frequency
- Python Pandas - Return the frequency object from the PeriodIndex object
- Python Pandas PeriodIndex - Convert the PeriodArray to the specified frequency
- 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 - Create a PeriodIndex and get the day of the year
- Python Pandas - Create a PeriodIndex and get the day of the month
- Python Pandas - Return the frequency object as a string from the PeriodIndex object
- Python Pandas - Convert PeriodIndex object to Timestamp
- Python Pandas - Get the year from the PeriodIndex object
- Python Pandas - Format the string representation of the PeriodIndex object
- Python Pandas - Create a Series from TimeDeltaIndex and set the name of the resulting Series
- Python Pandas - Create a Series from TimeDeltaIndex and set the index of the resulting Series
- Python Pandas - Get the hour of the period from the PeriodIndex object
- Python Pandas - Get the minute of the period from the PeriodIndex object

Advertisements