
- 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 get the day of the year
To create a PeriodIndex, use the pandas.PeriodIndex() method. Get the day of the year using the PeriodIndex.dayofyear 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 day of the year from the PeriodIndex object −
print("\nDays of the year from the PeriodIndex...\n", periodIndex.dayofyear)
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 day from the PeriodIndex object print("\nThe number of days from the PeriodIndex...\n", periodIndex.day) # Display day of the year from the PeriodIndex object print("\nDays of the year from the PeriodIndex...\n", periodIndex.dayofyear)
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> The number of days from the PeriodIndex... Int64Index([25, 30, 20, 15, 12, 18], dtype='int64') Days of the year from the PeriodIndex... Int64Index([206, 303, 325, 258, 71, 169], dtype='int64')
- Related Articles
- Python Pandas - Create a PeriodIndex and get the day of the month
- Python Pandas - Get the year from the PeriodIndex object
- 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 Day of the year from Period object
- Python Pandas - Create a PeriodIndex and set frequency
- 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
- Python Pandas - Indicate if the date from the PeriodIndex object belongs to a leap year
- Python Pandas - Get the month number of the period from the PeriodIndex object
- Python Pandas - Get the year from the Period object
- Python Pandas - Get the month of the year component of the Period

Advertisements