+

Python | Pandas DatetimeIndex.dayofweek

DatetimeIndex.dayofweek Pandas DatetimeIndex.dayofweek displays the ordinal of the day of the week for each record of the DatetimeIndex object. Values ​​from Monday to Sunday range from 0 to 6.

Syntax: DatetimeIndex.dayofweek

Return: Index object

Example # 1: Use the DatetimeIndex.dayofweek attribute to find the ordinal of the day of the week for each record in the DatetimeIndex object.

# import pandas as pd

import pandas as pd

  
# Create DatetimeIndex
# Here "D" represents the daily frequency

didx = pd.DatetimeIndex (start = `2000-01-10 06:30` , freq = ` D`

periods = 3 , tz = `Asia / Calcutta` )

 
# Print DatetimeIndex

print (didx)

Output:

Now we want to find the ordinal of the day of the week for each record in the DatetimeIndex object.

# find ordinal number of the day
# weeks for each post present in the object
didx.dayofweek

Output:

As we can see from the output, the function returned an Index object containing the ordinal values ​​of the days of weeks for each record of the DatetimeIndex object.

Example # 2: Use the DatetimeIndex.dayofweek attribute to find the ordinal of the day of the week for each record in the DatetimeIndex object.

# import pandas as pd

import pandas as pd

  
# Create DatetimeIndex
# Here" M "represents no monthly frequency

didx = pd. DatetimeIndex (start = `2014-08-01 10:05:45` , freq = ` M` ,

periods = 5 , tz = ` Asia / Calcutta` )

 
# Print DatetimeIndex

print (didx)

Output:

Now we want to find the ordinal n the number of the day of the week for each entry in the DatetimeIndex object.

# find the ordinal value of the day of the week
# for each record present in the object
didx.dayofweek

Output:

As we can see from the output, the function returned an Index object containing the ordinal values ​​of the days of the weeks for each record of the DatetimeIndex object.

Get Solution for free from DataCamp guru