Pandas DatetimeIndex property weekday in Python
The Pandas DatetimeIndex property weekday used to retrieve the day of the week from the given Timestamps where Monday represents 0 and Sunday represents 6. It returns Series or Index that includes integers that specifies the day number of the week.
Syntax
 1 DatetimeIndex.weekday
weekday property
 1 import pandas as pd
 2 
 3 dIndex = pd.DatetimeIndex(['2022-07-22 10:23', '2023-05-23 10:23'])
 4 
 5 print('The DatetimeIndex object :')
 6 print(dIndex)
 7 
 8 res = dIndex.weekday
 9 print('The day of week in DatetimeIndex object :')
 10 print(res)
In the above example, a DatetimeIndex object is created by passing an array of datetime. A day_of_week property is access that returns Index that includes day of week from specified Timestamps object. The result is assign to the variable that will be printed on console.
Output
 1 The DatetimeIndex object :
 2 DatetimeIndex(['2022-07-22 10:23:00', '2023-05-23 10:23:00'], 
 3                                 dtype='datetime64[ns]', freq=None)
 4 
 5 The day of week in DatetimeIndex object :
 6 Index([4, 1], dtype='int32')

Example 2

Example 2
 1 import pandas as pd
 2 
 3 dIndex = pd.date_range('2022-01-01 12:30:32',
 4 	periods = 3, freq = 'YE')
 5 
 6 print('The DatetimeIndex object :')
 7 print(dIndex)
 8 
 9 res = dIndex.weekday
 10 print('The day of week in DatetimeIndex object :')
 11 print(res)
In the above example, a DatetimeIndex object is created using date_range() with time. A day_of_week property is access that returns Index that includes day of week from specified Timestamps object. The result is assign to the variable that will be print.
Output
 1 The DatetimeIndex object :
 2 DatetimeIndex(['2022-12-31 12:30:32', '2023-12-31 12:30:32',
 3                '2024-12-31 12:30:32'],
 4               dtype='datetime64[ns]', freq='YE-DEC')
 5 
 6 The day of week in DatetimeIndex object :
 7 Index([5, 6, 1], dtype='int32')
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us