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

后端 未结 6 1676
深忆病人
深忆病人 2021-02-13 23:06

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 r

相关标签:
6条回答
  • 2021-02-13 23:24

    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

    0 讨论(0)
  • 2021-02-13 23:25

    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)
    
    0 讨论(0)
  • 2021-02-13 23:43

    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")
    
    0 讨论(0)
  • 2021-02-13 23:45

    There is builtin method to do this in pandas.

    For Pandas version <1.0

    from pandas.tseries.offsets import Day, BDay
    from datetime import date
    
    
    bdays=BDay()
    is_business_day = bday.onOffset(date(2020,8,20))
    

    For Pandas version >=1.1.0 (onOffset is deprecated)

    from pandas.tseries.offsets import Day, BDay
    from datetime import date
    
    
    bdays=BDay()
    is_business_day = bday.is_on_offset(date(2020,8,20))
    
    0 讨论(0)
  • 2021-02-13 23:48

    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)))
    
    0 讨论(0)
  • 2021-02-13 23:48

    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.

    0 讨论(0)
提交回复
热议问题