Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Python Pandas - Create a PeriodIndex and get the day of the year
A PeriodIndex in Pandas is an immutable array that holds ordinal values representing regular periods in time. You can create one using pd.PeriodIndex() and extract the day of the year using the dayofyear property.
Creating a PeriodIndex
First, import pandas and create a PeriodIndex with dates ?
import pandas as pd
# Create a PeriodIndex with daily frequency
periodIndex = pd.PeriodIndex(['2018-07-25', '2019-10-30', '2020-11-20',
'2021-09-15', '2022-03-12', '2023-06-18'], freq="D")
print("PeriodIndex:")
print(periodIndex)
PeriodIndex: PeriodIndex(['2018-07-25', '2019-10-30', '2020-11-20', '2021-09-15', '2022-03-12', '2023-06-18'], dtype='period[D]')
Getting Day of Year
Use the dayofyear property to get the ordinal day number within each year ?
import pandas as pd
periodIndex = pd.PeriodIndex(['2018-07-25', '2019-10-30', '2020-11-20',
'2021-09-15', '2022-03-12', '2023-06-18'], freq="D")
# Get day of year for each period
days_of_year = periodIndex.dayofyear
print("Days of the year:")
print(days_of_year)
Days of the year: Index([206, 303, 325, 258, 71, 169], dtype='int64')
Complete Example
Here's a comprehensive example showing PeriodIndex properties ?
import pandas as pd
# Create a PeriodIndex object
periodIndex = pd.PeriodIndex(['2018-07-25', '2019-10-30', '2020-11-20',
'2021-09-15', '2022-03-12', '2023-06-18'], freq="D")
print("PeriodIndex:")
print(periodIndex)
print("\nFrequency:")
print(periodIndex.freq)
print("\nDay of month:")
print(periodIndex.day)
print("\nDay of year:")
print(periodIndex.dayofyear)
PeriodIndex: PeriodIndex(['2018-07-25', '2019-10-30', '2020-11-20', '2021-09-15', '2022-03-12', '2023-06-18'], dtype='period[D]') Frequency: <Day> Day of month: Index([25, 30, 20, 15, 12, 18], dtype='int64') Day of year: Index([206, 303, 325, 258, 71, 169], dtype='int64')
Understanding the Results
The dayofyear values represent:
- July 25, 2018 ? day 206 (January-June = 181 days + 25 = 206)
- October 30, 2019 ? day 303
- November 20, 2020 ? day 325 (leap year)
- September 15, 2021 ? day 258
- March 12, 2022 ? day 71
- June 18, 2023 ? day 169
Conclusion
The PeriodIndex.dayofyear property returns the ordinal day of the year for each period. This is useful for time series analysis when you need to identify seasonal patterns or compare dates across different years.
Advertisements
