trying to set two dates: End Date and Begin Date. Neither can be on weekend and neither can be on Holiday. End date has to be the most recent business date (t-1) and Begin Date has to be the most recent business date before End date (t-2). The list of Holidays has to be maintained manually, seems all other resources available (Holiday library, workalendar library) aren't sufficient.
I tried and got stuck here:
import pandas as pd
import time
from datetime import datetime, date, time, timedelta
import csv
enddate = datetime.today() - timedelta(days = 1)
if datetime.weekday(enddate) == 6:
enddate = enddate - timedelta(days = 2)
elif datetime.weekday(enddate) == 5:
enddate = enddate - timedelta(days = 1)
begdate = enddate - timedelta(days = 1)
if datetime.weekday(begdate) == 6:
begdate = begdate - timedelta(days = 2)
elif datetime.weekday(begdate) == 5:
begdate = begdate - timedelta(days = 1)
holidaylist = pd.read_csv('holiday.csv')
Any help would be appreciated.
Best regards, Michael
CodePudding user response:
So this work for me. One downside is the list of Holidays have to be manually updated when required.
# Date Setup
holidaylist = pd.read_csv('holiday.csv')
holidaylist['Date'] = pd.to_datetime(holidaylist['Date'])
# enddate = date(2020, 12, 28)
enddate = datetime.today() - timedelta(days = 1)
while enddate in holidaylist.values or datetime.weekday(enddate) == 6 or datetime.weekday(enddate) == 5:
enddate = enddate - timedelta(days = 1)
begdate = enddate - timedelta(days = 1)
while begdate in holidaylist.values or datetime.weekday(begdate) == 6 or datetime.weekday(begdate) == 5:
begdate = begdate - timedelta(days = 1)
ed = enddate.strftime("%Y-%m-%d")
bd = begdate.strftime("%Y-%m-%d")
# print(ed,bd)