Saturday, November 16, 2024
Google search engine
HomeLanguagesHow to check whether the day is a weekday or not using...

How to check whether the day is a weekday or not using Pandas in Python?

Python is a very popular language because it is suitable for almost any type of data science task. And Pandas is one of the popular python-based data analysis toolkits and also provides pandas.bdate_range() function to return a fixed frequency DatetimeIndex. This function Returns a fixed frequency DatetimeIndex, with the business day(Mon to Fri) as the default frequency.

Syntax: pandas.bdate_range(start=None, end=None, periods=None, freq=’B’, tz=None, normalize=True, name=None, weekmask=None, holidays=None, closed=None, **kwargs)

Parameters :

start : string or datetime-like, default None.

end : string or datetime-like, default None.

periods : integer, default None.

freq :  string or DateOffset, default ‘B’ (business daily).

tz : string or None.

normalize : bool, default False

name : str, default None

weekmask : str or None, default None

holidays : list-like or None, default None

Approach : 

  • Import the Pandas module
  • Create a Parameter function that returns a boolean value
  • Check the given date is returning boolean with pd.bdate_range() inside function
  • Check if boolean is False then the date belongs to a weekday if boolean is true then is not weekday

Below is the implementation.

Python3




# importing Pandas module
import pandas as pd
  
# Creating a Function
def check_weekday(date):
      
    # computing the parameter date
    # with len function
    res=len(pd.bdate_range(date,date))
      
    if res == 0 :
        print("This is weekend")
    else:
        print("This is your working day"
  
# user input
date = "2020-08-17"
check_weekday(date)
  
date = "2020-08-16"
check_weekday(date)


Output :

This is your working day
This is weekend
RELATED ARTICLES

Most Popular

Recent Comments