Date and time play an important role in python. We may have to deal with time-series data or columns which contain a timestamp. There are many modules that allow us to work with date and time values.
In this article, we will discuss how to get day of week in Python.
Table of Contents
Using the datetime.weekday()
function
Date
is not a defined datatype in Python. However, we can work with date objects by using the datetim
e module. This module allows us to create and work with such objects. The weekday()
function from this module is used to get day of week from datetime in Python.
For example,
1 2 3 4 5 |
import datetime d = datetime.datetime(2021,5,16).weekday() print(d) |
Output:
In the above example datetime()
constructor is used to initiating a date object. The weekday()
function returns a value from 0 to 6. Here 0 is considered as Monday as the start of the week and 6 as the last day of the week that is Sunday.
We can add a small line of code to get the name of the day from this number.
1 2 3 4 5 6 |
import datetime d = datetime.datetime(2021,5,16).weekday() lst = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] print(lst[d]) |
Output:
In the above code, we created a list of the name of days and every day was at its corresponding value based on the weekday()
function. This is best way to get day of week in Python.
Using the datetime.isoweekday()
function
This function works similar to the weekday()
function mentioned in the previous method. However, it returns 1 for the first day of the week that is Monday, and 7 for the last day.
We can use the above list method to get the name of the day by just making a slight alteration to the code.
For example,
1 2 3 4 5 6 |
import datetime d = datetime.datetime(2021,5,16).isoweekday() ##d = 7 lst = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] print(lst[d-1]) |
Output:
Using the datetime.strftime()
function
The strftime()
function is used to return some string based on the format from a given date. We can use it get the weekday from a date as shown below.
1 2 3 4 5 |
import datetime d = datetime.datetime(2021,5,16) print (d.strftime("%A")) |
Output:
Note that in this method we can use the
%a
format to get the weekday name in short and the%w
format to get the day number.
Using the calendar.weekday()
function
The calendar module can be used for our problem as an alternative. This module is equipped with different functions for calendar related operations. The weekday()
function from this module works similarly to the one in the datetime module.
For example,
1 2 3 4 5 |
import calendar d = calendar.weekday(2021,5,16) print(d) |
Output:
We can use the day_name
object from this module to get the name of the weekday. It works similar to the list method discussed earlier in the previous methods.
1 2 3 4 5 |
import calendar d = calendar.weekday(2021,5,16) print(calendar.day_name[d]) |
Output:
Using the pandas.dayofweek()
function
The pandas library deals with data containing dates very frequently in time-series data or Date columns. Therefore the module is well equipped to work with such values.
The dayofweek()
function from this module returns the day of week of a given date. It also considers that the week starts with Monday and returns 0 for it and 6 for Sunday.
For example,
1 2 3 4 5 |
import pandas as pd d = pd.Timestamp("2021-05-16") print(d.dayofweek) |
Output:
Note that we use the
pandas.Timestamp()
to return a datetime object. This function works only for datetime values and can provide the day of the week for DataFrames or Series objects at once. We can use the list method discussed in the earlier methods to get the name of the day also.
Using the pandas.weekday_name()
function
The weekday_name()
function returns the name of the day from a given date. It works for DataFrames and Series objects also.
For example,
1 2 3 4 5 |
import pandas as pd d = pd.Timestamp("2021-05-16") print(d.weekday_name) |
Output:
Note that this function is deprecated in the recent versions of the module so should be avoided.
Using dateutil.parser()
function
This function is also used to format or parse dates. It can return the weekday using the strftime()
function by passing the required format to the function.
For example,
1 2 3 4 5 |
from dateutil import parser##7 day = parser.parse('May 16, 2021').strftime("%A") print(day) |
Output:
Note that the format of the given date can be altered as per the user and the result will be the same.
Using a user-defined function
If one has to avoid all the modules discussed above at all costs, then we can create our own little function which will return the number of the weekday from a given day.
This method assumes that the change to the Georgian Calendar occurred in the year 1582
and uses that value.
See the code below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
def dayofweek(year,month,day): x, y = month, day if x == 1: x = 13 year -= 1 elif x == 2: x = 14 year -= 1 rem = year % 100 div = year // 100 f = (y + int(13*(x + 1)/5.0) + rem + int(rem/4.0)) fg = f + int(div/4.0) - 2 * div fdiv = f + 5 - div if year > 1582: h = fg % 7 else: h = fdiv % 7 if h == 0: h = 7 return h print(dayofweek(2021,5,16)) |
Output:
In the above method, we consider Sunday as the first day of the week and assign it as 1 and Saturday as the last day with value as 7.
That’s all about how to get day of week in Python.