The Pandas PeriodIndex property dayofweek used to retrieve the day of week for the given PeriodIndex object. It returns an Index that includes number that represents the day of week for each elements.
Note : It represents Monday = 0 and Sunday = 6.
1 import pandas as pd
2
3 pIndex = pd.PeriodIndex(['2023-07-25', '2023-8-30' , '2023-9-20'], freq = 'D')
4
5 print('The original PeriodIndex object :')
6 print(pIndex)
7
8 res = pIndex.dayofweek
9 print('The day of week in PeriodIndex :')
10 print(res)
In the above example, a PeriodIndex object is created by passing an array of dates and freq is D. A dayofweek property is access that returns array that includes day of week where Monday = 0 and Sunday = 6. The result is assign to the variable that will be printed on console.
1 The original PeriodIndex object :
2 PeriodIndex(['2023-07-25', '2023-08-30', '2023-09-20'], dtype='period[D]')
3
4 The day of week in PeriodIndex :
5 Index([1, 2, 2], dtype='int64')
Example 2
1 import pandas as pd
2
3 dIndex = pd.date_range('2022-01-01 12:30:32',
4 periods = 3, freq = 'D')
5 pIndex = pd.PeriodIndex(dIndex)
6
7 print('The original PeriodIndex object :')
8 print(pIndex)
9
10 res = pIndex.dayofweek
11 print('The day of week in PeriodIndex :')
12 print(res)
In the above example, a PeriodIndex object is created using date_range() with time and freq is D. A dayofweek property is access that returns array that includes day of week where Monday = 0 and Sunday = 6. The result is assign to the variable that will be print.
1 The original PeriodIndex object :
2 PeriodIndex(['2022-01-01', '2022-01-02', '2022-01-03'], dtype='period[D]')
3
4 The day of week in PeriodIndex :
5 Index([5, 6, 0], dtype='int64')
Related options for your search