
- 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 - Extract the day of week from the DateTimeIndex with specific time series frequency
To extract the day of week from the DateTimeIndex with specific time series frequency, use the DateTimeIndex.dayofweek property
At first, import the required libraries −
import pandas as pd
Create a DatetimeIndex with period 6 and frequency as D i.e. day −
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Sydney', freq='3D')
Display DateTimeIndex frequency −
print("DateTimeIndex frequency...\n", datetimeindex.freq)
Get the day of week. It is assumed the week starts on Monday, which is denoted by 0 and ends on Sunday which is denoted by 6 −
print("\nGet the day of week..\n",datetimeindex.dayofweek)
Example
Following is the code −
import pandas as pd # DatetimeIndex with period 6 and frequency as D i.e. day # The timezone is Australia/Sydney datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Sydney', freq='3D') # display DateTimeIndex print("DateTimeIndex...\n", datetimeindex) # display DateTimeIndex frequency print("DateTimeIndex frequency...\n", datetimeindex.freq) # Get the day of week # It is assumed the week starts on Monday, # which is denoted by 0 and ends on Sunday which is denoted by 6 print("\nGet the day of week..\n",datetimeindex.dayofweek)
Output
This will produce the following code −
DateTimeIndex... DatetimeIndex(['2021-10-20 02:30:50+11:00', '2021-10-23 02:30:50+11:00', '2021-10-26 02:30:50+11:00', '2021-10-29 02:30:50+11:00', '2021-11-01 02:30:50+11:00', '2021-11-04 02:30:50+11:00'], dtype='datetime64[ns, Australia/Sydney]', freq='3D') DateTimeIndex frequency... <3 * Days> Get the day of week.. Int64Index([2, 5, 1, 4, 0, 3], dtype='int64')
- Related Articles
- Python Pandas - Extract the day from the DateTimeIndex with specific time series frequency
- Python Pandas - Extract the ordinal day of year from the DateTimeIndex with specific time series frequency
- Python Pandas - Extract the minute from DateTimeIndex with specific time series frequency
- Python Pandas - Extract year from the DateTimeIndex with specific time series frequency
- Python Pandas - Extract the hour from the DateTimeIndex with specific time series frequency
- Python Pandas - Extract the seconds from the DateTimeIndex with specific time series frequency
- Python Pandas - Extract the microseconds from the DateTimeIndex with specific time series frequency
- Python Pandas - Extract the nanoseconds from the DateTimeIndex with specific time series frequency
- Python Pandas - Extract the timezone from the DateTimeIndex with specific time series frequency
- Python Pandas - Extract month number from the DateTimeIndex with specific time series frequency
- Python Pandas - Extract the quarter of the date from the DateTimeIndex with specific time series frequency
- Python Pandas - Extract the frequency from the DateTimeIndex
- Python Pandas - Extract the frequency object as a string from the DateTimeIndex
- Python Pandas - Get the day of the week from the PeriodIndex object
- Python Pandas - How to Round the DateTimeIndex with hourly frequency

Advertisements