I need to subtract business days from the current date.
I currently have some code which needs always to be running on the most recent business day. So that may be today if we’re Monday thru Friday, but if it’s Saturday or Sunday then I need to set it back to the Friday before the weekend. I currently have some pretty clunky code to do this:
lastBusDay = datetime.datetime.today() if datetime.date.weekday(lastBusDay) == 5: #if it's Saturday lastBusDay = lastBusDay - datetime.timedelta(days = 1) #then make it Friday elif datetime.date.weekday(lastBusDay) == 6: #if it's Sunday lastBusDay = lastBusDay - datetime.timedelta(days = 2); #then make it Friday
Is there a better way?
Can I tell timedelta to work in weekdays rather than calendar days for example?
import datetime # BDay is business day, not birthday... from pandas.tseries.offsets import BDay today = datetime.datetime.today() print(today - BDay(4))
Since today is Thursday, Sept 26, that will give you an output of:
datetime.datetime(2013, 9, 20, 14, 8, 4, 89761)
There seem to be several options if you’re open to installing extra libraries.
This post describes a way of defining workdays with dateutil.
BusinessHours lets you custom-define your list of holidays, etc., to define when your working hours (and by extension working days) are.
If you want to skip US holidays as well as weekends, this worked for me (using pandas 0.23.3):
import pandas as pd from pandas.tseries.holiday import USFederalHolidayCalendar from pandas.tseries.offsets import CustomBusinessDay US_BUSINESS_DAY = CustomBusinessDay(calendar=USFederalHolidayCalendar()) july_5 = pd.datetime(2018, 7, 5) result = july_5 - 2 * US_BUSINESS_DAY # 2018-7-2
To convert to a python date object I did this:
Maybe this code could help:
lastBusDay = datetime.datetime.today() shift = datetime.timedelta(max(1,(lastBusDay.weekday() + 6) % 7 - 3)) lastBusDay = lastBusDay - shift
The idea is that on Mondays yo have to go back 3 days, on Sundays 2, and 1 in any other day.
(lastBusDay.weekday() + 6) % 7 just re-bases the Monday from 0 to 6.
Really don’t know if this will be better in terms of performance.
DISCLAMER: I’m the author…
I wrote a package that does exactly this, business dates calculations. You can use custom week specification and holidays.
I had this exact problem while working with financial data and didn’t find any of the available solutions particularly easy, so I wrote one.
Hope this is useful for other people.
timeboard package does this.
Suppose your date is 04 Sep 2017. In spite of being a Monday, it was a holiday in the US (the Labor Day). So, the most recent business day was Friday, Sep 1.
import timeboard.calendars.US as US clnd = US.Weekly8x5() clnd('04 Sep 2017').rollback().to_timestamp().date() datetime.date(2017, 9, 1)
In UK, 04 Sep 2017 was the regular business day, so the most recent business day was itself.
import timeboard.calendars.UK as UK clnd = UK.Weekly8x5() clnd('04 Sep 2017').rollback().to_timestamp().date() datetime.date(2017, 9, 4)
DISCLAIMER: I am the author of timeboard.
If somebody is looking for solution respecting holidays (without any huge library like pandas), try this function:
import holidays import datetime def previous_working_day(check_day_, holidays=holidays.US()): offset = max(1, (check_day_.weekday() + 6) % 7 - 3) most_recent = check_day_ - datetime.timedelta(offset) if most_recent not in holidays: return most_recent else: return previous_working_day(most_recent, holidays) check_day = datetime.date(2020, 12, 28) previous_working_day(check_day)
datetime.date(2020, 12, 24)
This will give a generator of working days, of course without holidays, stop is datetime.datetime object. If you need holidays just make additional argument with list of holidays and check with ‘IFology’ 😉
def workingdays(stop, start=datetime.date.today()): while start != stop: if start.weekday() < 5: yield start start += datetime.timedelta(1)
Later on you can count them like
workdays = workingdays(datetime.datetime(2015, 8, 8)) len(list(workdays))