Python – Get Most recent previous business day
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
Please Login to comment...