Python – Get Most recent previous business day
Last Updated :
27 Mar, 2023
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
from datetime import datetime, timedelta
test_date = datetime( 2020 , 1 , 31 )
print ( "The original date is : " + str (test_date))
diff = 1
if test_date.weekday() = = 0 :
diff = 3
elif test_date.weekday() = = 6 :
diff = 2
else :
diff = 1
res = test_date - timedelta(days = diff)
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
from datetime import datetime, timedelta
test_date = datetime( 2020 , 1 , 31 )
print ( "The original date is : " + str (test_date))
diff = max ( 1 , (test_date.weekday() + 6 ) % 7 - 3 )
res = test_date - timedelta(days = diff)
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
import pandas as pd
from datetime import datetime
test_date = datetime( 2020 , 2 , 3 )
print ( "The original date is : " + str (test_date))
ts = pd.Timestamp( str (test_date))
offset = pd.tseries.offsets.BusinessDay(n = 1 )
res = test_date - offset
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
Share your thoughts in the comments
Please Login to comment...