Calculate date 5 days from today, adding an extra day for each day in the next 5 days that is a weekend day

杀马特。学长 韩版系。学妹 提交于 2019-12-06 05:54:21

Basically you need to add 5 business days... This should do it:

import datetime
def addBusinessDays(from_date, add_days):
    business_days_to_add = add_days
    current_date = from_date
    while business_days_to_add > 0:
        current_date += datetime.timedelta(days=1)
        weekday = current_date.weekday()
        if weekday >= 5: # sunday = 6
            continue
        business_days_to_add -= 1
    return current_date

#demo:
print '5 business days from today:'
print addBusinessDays(datetime.date.today(), 5)

Update:

Here is the explanation:

  1. We get the start date(Date that we need to add business days to it)
  2. We use a loop to add days 1 at a time to the date(we use datetime.timedelta(days=1) to add 1 day to date)
  3. After adding each day we check to see if updated date is weekday. If it's weekday we count it otherwise we don't count it and continue

You can do it mathematically:

from datetime import timedelta

def business_days(date, days):
    if days == 0:
        return date
    day = date.weekday()
    if day in (5, 6):
        date += timedelta(days=7 - day)
        days -= 1
    date += timedelta(days=days / 5 * 7)
    return date + timedelta(days=days % 5)

It will work to get a date n business days from or since whatever date you pass in:

In [6]: dte = datetime.datetime.today()

In [7]: business_days(dte, 5)
Out[7]: datetime.datetime(2016, 1, 6, 11, 28, 38, 264331)

In [8]: business_days(dte, -5)
Out[8]: datetime.datetime(2015, 12, 23, 11, 28, 38, 264331)

Using NumPy, you can find add or subtract business days using np.busday_offset:

Since 2016-03-27 is a Sunday, rolling forward returns the first valid business day, 2016-03-28:

import numpy as np
np.busday_offset('2016-03-27', 0, roll='forward')
# numpy.datetime64('2016-03-28')

To obtain a datetime object, call item():

np.busday_offset('2016-03-27', 0, roll='forward').item()
# datetime.date(2016, 3, 28)

To advance 5 business days, change the second argument to 5:

np.busday_offset('2016-03-27', 5, roll='forward').item()
# datetime.date(2016, 4, 4)

To go back 5 business days, use a negative offset and roll='backward':

np.busday_offset('2016-03-27', -5, roll='backward').item()
# datetime.date(2016, 3, 18)

np.busday_offset('2016-03-28', -5, roll='backward').item()
# datetime.date(2016, 3, 21)

Here's a calendar for reference:

      March 2016               April 2016         
 Su Mo Tu We Th Fr Sa     Su Mo Tu We Th Fr Sa    
        1  2  3  4  5                     1  2    
  6  7  8  9 10 11 12      3  4  5  6  7  8  9    
 13 14 15 16 17 18 19     10 11 12 13 14 15 16    
 20 21 22 23 24 25 26     17 18 19 20 21 22 23    
 27 28 29 30 31           24 25 26 27 28 29 30    

If you want to work with a lot of timeseries, have a look at pandas. Adding five business days is quite straightforward:

from datetime import datetime
import pandas as pd

today = datetime.today()
then = today + pd.tseries.offsets.BDay(5)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!