Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if a day is a business day in Python / Pandas

I currently have a program setup to run two different ways. One way is to run over a specified time frame, and the other way is to run everyday. However, when I have it set to run everyday, I only want it to continue if its a business day. Now from research I've seen that you can iterate through business days using Pandas like so:

start = 2016-08-05
end = datetime.date.today().strftime("%Y-%m-%d")

for day in pd.bdate_range(start, end):
    print str(day) + " is a business Day"

And this works great when I run my program over the specified period.

But when I want to have the program ran everyday, I can't quite figure out how to test one specific day for being a business day. Basically I want to do something like this:

start = datetime.date.today().strftime("%Y-%m-%d")
end = datetime.date.today().strftime("%Y-%m-%d")

if start == end:
    if not Bdate(start)
        print "Not a Business day"

I know I could probably setup pd.bdate_range() to do what I'm looking for, but in my opinion would be sloppy and not intuitive. I feel like there must be a simpler solution to this. Any advice?

like image 303
Justin Avatar asked Aug 21 '16 18:08

Justin


People also ask

What are pandas business days?

Dateoffsets are a standard kind of date increment used for a date range in Pandas. It works exactly like relativedelta in terms of the keyword args we pass in. DateOffets work as follows, each offset specify a set of dates that conform to the DateOffset.


Video Answer


1 Answers

Since len of pd.bdate_range() tells us how many business days are in the supplied range of dates, we can cast this to a bool to determine if a range of a single day is a business day:

def is_business_day(date):
    return bool(len(pd.bdate_range(date, date)))
like image 160
Zev Averbach Avatar answered Sep 21 '22 08:09

Zev Averbach