
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check Whether the Day is a Weekday or Not Using Pandas in Python
The Python pandas library provides different functions to check whether the day is a weekday or not namely, weekday(), day_name(), isoweekday() etc.,
Pandas is an open source python library which is used for data manipulation, data analysis, data visualization, data structures etc. This library is widely used in data science and data analysis which provides data cleaning, exploration and transformation. There are many modules and functions available in pandas to work with the given data.
Using the weekday() function
In python, the weekday() function can be used to determine whether a given day is a weekday or not. This function returns the day of the week in the form of an integer. Where, 0 indicates Monday, 1 indicates Tuesday and so on.
Example
In the following example, we will apply the weekday() function to the date created by the pandas dataFrame. The resulting output will indicate whether the given date is a weekday or not.
import pandas as pd date = pd.to_datetime('2023-03-25') print(date.weekday()) if date.weekday() < 5: print(date,"is Weekday") else: print(date,"is Weekend")
Output
The following is the output of the weekday() function.
5 2023-03-25 00:00:00 is Weekend
Example
Let's see another example to understand the weekday() function of the python library.
def week_day(date): import pandas as pd dte = pd.to_datetime(date) print(dte.weekday()) if dte.weekday() < 5: print(dte,"is Weekday") else: print(dte,"is Weekend") week_day('23-03-2023')
Output
The following is the output of the weekday() function, when we run the above code --
3 2023-03-23 00:00:00 is Weekday
Using the day_name() function
The day_name() function can also be used to check whether the given day is a weekday or weekend. This function returns the day as the string such as weekday or weekend.
Example
In this example, we will apply the day_name() function on the date we created using the to_datetime() function of the pandas library.
import pandas as pd date = pd.to_datetime('2023-03-25') print(date.day_name()) if date.day_name() in ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']: print(date,"is Weekday") else: print(date,"is Weekend")
Output
The below is the output of the day_name() function to find the weekday or weeend.
Saturday 2023-03-25 00:00:00 is Weekend
Example
This is one more example of the day_name() function used to find the given date is weekday or weekend.
def week_date(date): import pandas as pd dte = pd.to_datetime(date) print(dte.day_name()) if dte.day_name() in ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']: print(dte,"is Weekday") else: print(dte,"is Weekend") week_date('14-05-2023')
Output
When we run the above code, following output will be generated -
Sunday 2023-05-14 00:00:00 is Weekend
Using isoweekday() function
Another way to check whether the specified date is a weekday or weekend is by using the isoweekday() function of python. This function returns the day of the week as the integer where monday as 1, Tuesday as 2, wednesday as 3, Thursday as 4, Friday as 5, saturday as 6 and sunday as 7.
import pandas as pd dte = pd.to_datetime('2023-03-25') print(dte.isoweekday()) if dte.isoweekday() < 6: print(dte,"is Weekday") else: print(dte,"is Weekend")
Output
The following is the output of the isoweekday() function to check the given date is weekday or weekend.
6 2023-03-25 00:00:00 is Weekend
Example
Let's see another example to understand the working of the day_name() which checks the given date is a weekday or weekend. The following is the code.
def wee_day(date): import pandas as pd dte = pd.to_datetime(date) print(dte.isoweekday()) if dte.isoweekday() < 6: print(dte,"is Weekday") else: print(dte,"is Weekend") wee_day('2022-11-21')
Output
The below is the output of the day_name() function used to check the given date is weekend or weekday.
1 2022-11-21 00:00:00 is Weekday
Example
Following is another example -
a = 300 b = 20 c = 30 d = 340 dic = {"a" : 10,"b" : 20,"c" : 40,"d" : 230} if any([dic[key] == value for key, value in locals().items() if key in dic]): print("At least one variable is equal to their respective value") else: print("All the variables are not equal to their respective values")
Output
The below is the output of the variables and values in dictionary using the list comprehension and locals() function.
At least one variable is equal to their respective value