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
Python Pandas - Create a PeriodIndex and get the days of the week
To create a PeriodIndex, use the pandas.PeriodIndex() method. Get the days of the week using the PeriodIndex.dayofweek property, which returns Monday=0, Tuesday=1 ... Sunday=6.
What is PeriodIndex?
PeriodIndex is an immutable ndarray holding ordinal values indicating regular periods in time. It's useful for time series analysis where you need to represent fixed-frequency date periods ?
Creating a PeriodIndex
First, import pandas and create a PeriodIndex with specific dates ?
import pandas as pd
# Create a PeriodIndex object 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 Days of the Week
Use the dayofweek property to extract the day of the week as integers ?
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 days of the week (Monday=0, Tuesday=1, ..., Sunday=6)
days_of_week = periodIndex.dayofweek
print("Days of the week from the PeriodIndex...")
print(days_of_week)
Days of the week from the PeriodIndex... Index([2, 2, 4, 2, 5, 6], dtype='int64')
Complete Example
Here's a comprehensive example showing PeriodIndex creation and various properties ?
import pandas as pd
# Create a PeriodIndex object 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")
# Display PeriodIndex object
print("PeriodIndex...")
print(periodIndex)
# Display frequency
print("\nPeriodIndex frequency...")
print(periodIndex.freq)
# Display day numbers
print("\nThe day numbers from the PeriodIndex...")
print(periodIndex.day)
# Display days of the week
print("\nDays of the week from the PeriodIndex...")
print(periodIndex.dayofweek)
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 day numbers from the PeriodIndex... Index([25, 30, 20, 15, 12, 18], dtype='int64') Days of the week from the PeriodIndex... Index([2, 2, 4, 2, 5, 6], dtype='int64')
Understanding the Output
The dayofweek values correspond to:
- 0 = Monday
- 1 = Tuesday
- 2 = Wednesday
- 3 = Thursday
- 4 = Friday
- 5 = Saturday
- 6 = Sunday
So our dates [2, 2, 4, 2, 5, 6] represent Wednesday, Wednesday, Friday, Wednesday, Saturday, and Sunday respectively.
Conclusion
Use pd.PeriodIndex() to create time period objects and dayofweek property to extract weekday information as integers from 0-6. This is useful for time series analysis and filtering data by weekdays.
