Open In App

Python – Get Most recent previous business day

Improve
Improve
Like Article
Like
Save
Share
Report

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 dates
test_date = datetime(2020, 1, 31)
              
# printing original date
print("The original date is : " + str(test_date))
 
# getting difference
diff = 1
if test_date.weekday() == 0:
    diff = 3
elif test_date.weekday() == 6:
    diff = 2
else :
    diff = 1
     
# subtracting diff
res = test_date - timedelta(days=diff)
 
# printing result
print("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 dates
test_date = datetime(2020, 1, 31)
              
# printing original date
print("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 result
print("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 pd
from datetime import datetime
 
# initializing dates
test_date = datetime(2020, 2, 3)
 
# printing original date
print("The original date is : " + str(test_date))
 
# Creating Timestamp
ts = pd.Timestamp(str(test_date))
 
# Create an offset of 1 Business days
offset = pd.tseries.offsets.BusinessDay(n=1)
 
# getting result by subtracting offset
res = test_date - offset
 
# printing result
print("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

Method 4: Using a while loop and timedelta:

Approach:

Import the necessary modules: datetime and timedelta from the datetime module.
Create a datetime object representing the test date.
Use a while loop to iterate through dates until a weekday is reached. If the current day is a weekend day (Saturday or Sunday), subtract one day from the date.
After the loop is exited, the test_date will now represent the most recent weekday before the original test date.
Subtract one day from the most recent weekday to get the most recent previous business day.
Assign the most recent previous business day to a variable.
Print the variable to output the result.

Python3




from datetime import datetime, timedelta
 
test_date = datetime(2020, 1, 31)
 
while test_date.weekday() >= 5:
    test_date -= timedelta(days=1)
 
most_recent_previous_business_day = test_date - timedelta(days=1)
 
print(most_recent_previous_business_day)


Output

2020-01-30 00:00:00

Time complexity: O(n) where n is the number of weekend days between the test_date and the most recent previous business day
Auxiliary Space: O(1



Last Updated : 27 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads