17

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?

Justin
  • 545
  • 3
  • 7
  • 17
  • 1
    The hardest part of the task is defining what a business day is. It might differ by country and in rare situations even by village. – Klaus D. Aug 21 '16 at 18:46
  • Thanks for the comment Klaus D. I guess to add some details: this program is specific to US business days. Specifically days that the market (NYSE,NASDAQ, etc) are open and the trading of stocks occur – Justin Aug 21 '16 at 18:49
  • 1
    Please check this module. It might help you. https://pypi.python.org/pypi/bdateutil/0.1 – Dinesh Pundkar Aug 21 '16 at 18:51
  • 1
    So, how are these days defined? If you have the answer you just have to put it in a function. – Klaus D. Aug 21 '16 at 18:51
  • Please check this link also - http://stackoverflow.com/questions/2224742/business-days-in-python – Dinesh Pundkar Aug 21 '16 at 18:54
  • I'm having a hard time understanding how this could ever return `False`? `if start == end:` – roganjosh Aug 21 '16 at 19:37

6 Answers6

28

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)))
Zev Averbach
  • 1,044
  • 1
  • 11
  • 25
7

I just found a different solution to this. This might be interesting if you want to find the next business day if your date is not a business day.

   bdays=BDay()
   def is_business_day(date):
       return date == date + 0*bdays

adding 0*bdays rolls forward on the next business day including the current one. Unfortunately, subtracting 0*bdays does not roll backwards (at least with the pandas version I was using).

Moreover, due to this behavior, you also need to be careful since not necessarily 0*bdays + 1*bdays != 1*bdays

Quickbeam2k1
  • 5,287
  • 2
  • 26
  • 42
6

There is builtin method to do this in pandas.

For Pandas version <1.0

from pandas.tseries.offsets import Day, BDay
from datetime import datetime


bday=BDay()
is_business_day = bday.onOffset(datetime(2020,8,20))

For Pandas version >=1.1.0 (onOffset is deprecated)

from pandas.tseries.offsets import Day, BDay
from datetime import datetime


bday=BDay()
is_business_day = bday.is_on_offset(datetime(2020,8,20))
rbonallo
  • 970
  • 1
  • 9
  • 21
Raghava Dhanya
  • 959
  • 15
  • 22
2

Using at least numpy version 1.7.0., try np.is_busday()

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

if start == end:

    # added code here
    if not np.is_busday(start):
        print("Not a Business day")
Ian Thompson
  • 2,914
  • 2
  • 18
  • 31
2

for me I use an old trick from Excel:

from pandas.tseries.offsets import Day, BDay

def is_bday(x):
    return x == x + Day(1) - BDay(1)
gioxc88
  • 503
  • 3
  • 18
0

Please check this module - bdateutil

Please check the below code using above module :

from bdateutil import isbday
from datetime import datetime,date
now = datetime.now()
val = isbday(date(now.year, now.month, now.day))
print val

Please let me know if this help.

Dinesh Pundkar
  • 4,160
  • 1
  • 23
  • 37