Python – Get Most recent previous business day

18 Jul, 2021
Given a date, the task is to write a Python program to get the most recent previous business day from the given date.
Example:
Input : test_date = datetime(2020, 1, 31)
Output : 2020-01-30 00:00:00
Explanation : 31 Jan 2020, being a Friday, last business day is thursday, i.e 30 January.
Input : test_date = datetime(2020, 2, 3)
Output : 2020-01-31 00:00:00
Explanation : 3 Feb 2020, being a Monday, last business day is friday, i.e 31 January.
Method 1: Using timedelta() + weekday()
In this, we perform the task of subtracting 3 in the case of Monday, 2 in the case of Sunday, and 1 on all other days. The timedelta() performs the task of subtraction, and conditional statements check for a weekday.
Python3
# Python3 code to demonstrate working of# Last business day# using timedelta() + conditional statements + weekday()from datetime import datetime, timedelta  # initializing datestest_date = datetime(2020, 1, 31)               # printing original dateprint("The original date is : " + str(test_date))  # getting differencediff = 1if test_date.weekday() == 0:    diff = 3elif test_date.weekday() == 6:    diff = 2else :    diff = 1      # subtracting diff res = test_date - timedelta(days=diff)  # printing resultprint("Last business day : " + str(res))
Output:
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
Method 2: Using max() + % operator + timedelta() 
Perform tasks in similar way, only difference being method to compute difference changes to get max() and % operator. 
Python3
# Python3 code to demonstrate working of# Last business day# using max() + % operator + timedelta() from datetime import datetime, timedelta  # initializing datestest_date = datetime(2020, 1, 31)               # printing original dateprint("The original date is : " + str(test_date))  # getting difference# using max() to get differences diff = max(1, (test_date.weekday() + 6) % 7 - 3)      # subtracting diff res = test_date - timedelta(days=diff)  # printing resultprint("Last business day : " + str(res))
Output:
The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00
Method 3 : Using pd.tseries.offsets.BusinessDay(n) 
In this, we create a Business day offset of 1 day, and subtract from the date initialized. This returns the previous business day as desired.
Python3
# Python3 code to demonstrate working of# Last business day# using pd.tseries.offsets.BusinessDay(n)import pandas as pdfrom datetime import datetime  # initializing datestest_date = datetime(2020, 2, 3)  # printing original dateprint("The original date is : " + str(test_date))  # Creating Timestampts = pd.Timestamp(str(test_date))  # Create an offset of 1 Business daysoffset = pd.tseries.offsets.BusinessDay(n=1)  # getting result by subtracting offsetres = test_date - offset  # printing resultprint("Last business day : " + str(res))
Output : 
The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00
Python datetime-program
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
Python program to convert a list to string
Python | Get dictionary keys as a list
Python | Convert a list to dictionary
Python Program for Fibonacci numbers
Python | Split string into list of characters