Get day of week in Python

Get day of week in Python

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.

Using the datetime.weekday() function

Date is not a defined datatype in Python. However, we can work with date objects by using the datetime 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,

Output:

6

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.

Output:

Sunday

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,

Output:

Sunday

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.

Output:

Sunday

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,

Output:

6

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.

Output:

Sunday

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,

Output:

6

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,

Output:

Sunday

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,

Output:

Sunday

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.

Output:

7

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.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *