Python Pandas - Extract the day of week from the DateTimeIndex with specific time series frequency

To extract the day of week from a DateTimeIndex with specific time series frequency, use the DateTimeIndex.dayofweek property. This property returns integers from 0-6 representing Monday through Sunday.

Creating a DateTimeIndex with Frequency

First, let's create a DateTimeIndex with a specific frequency using pd.date_range() ?

import pandas as pd

# Create DatetimeIndex with period 6 and frequency 3D (every 3 days)
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Sydney', freq='3D')

print("DateTimeIndex...")
print(datetimeindex)
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')

Checking the Frequency

You can verify the frequency of the DateTimeIndex using the freq attribute ?

import pandas as pd

datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Sydney', freq='3D')

print("DateTimeIndex frequency:")
print(datetimeindex.freq)
DateTimeIndex frequency:
<3 * Days>

Extracting Day of Week

Use the dayofweek property to extract the day of week. The week starts on Monday (0) and ends on Sunday (6) ?

import pandas as pd

datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Sydney', freq='3D')

print("Day of week (0=Monday, 6=Sunday):")
print(datetimeindex.dayofweek)
Day of week (0=Monday, 6=Sunday):
Int64Index([2, 5, 1, 4, 0, 3], dtype='int64')

Complete Example

Here's a complete example showing the DateTimeIndex creation and day of week extraction ?

import pandas as pd

# Create DatetimeIndex with period 6 and frequency as 3D (every 3 days)
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Sydney', freq='3D')

# Display DateTimeIndex
print("DateTimeIndex...")
print(datetimeindex)

# Display frequency
print("\nDateTimeIndex frequency...")
print(datetimeindex.freq)

# Get the day of week (0=Monday, 6=Sunday)
print("\nDay of week...")
print(datetimeindex.dayofweek)
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>

Day of week...
Int64Index([2, 5, 1, 4, 0, 3], dtype='int64')

Understanding the Results

The output shows that:

  • 2021-10-20 is a Wednesday (2)
  • 2021-10-23 is a Saturday (5)
  • 2021-10-26 is a Tuesday (1)
  • 2021-10-29 is a Friday (4)
  • 2021-11-01 is a Monday (0)
  • 2021-11-04 is a Thursday (3)

Conclusion

The dayofweek property provides an efficient way to extract day of week information from DateTimeIndex objects. This is particularly useful for time series analysis where you need to identify patterns based on weekdays.

Updated on: 2026-03-26T17:07:32+05:30

343 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements