1

Is there a (more) convenient/efficient method to calculate the number of business days between to dates using pandas?

I could do

len(pd.bdate_range(start='2018-12-03',end='2018-12-14'))-1 # minus one only if end date is a business day

but for longer distances between the start and end day this seems rather inefficient.

There are a couple of suggestion how to use the BDay offset object, but they all seem to refer to the creation of dateranges or something similar.

I am thinking more in terms of a Timedelta object that is represented in business-days.

Say I have two series,s1 and s2, containing datetimes. If pandas had something along the lines of

s1.dt.subtract(s2,freq='B') 
# giving a new series containing timedeltas where the number of days calculated
# use business days only

would be nice.

(numpy has a busday_count() method. But I would not want to convert my pandas Timestamps to numpy, as this can get messy.)

user3820991
  • 2,310
  • 5
  • 23
  • 32

3 Answers3

1

I think np.busday_count here is good idea, also convert to numpy arrays is not necessary:

s1 = pd.Series(pd.date_range(start='05/01/2019',end='05/10/2019'))
s2 = pd.Series(pd.date_range(start='05/04/2019',periods=10, freq='5d'))

s = pd.Series([np.busday_count(a, b) for a, b in zip(s1, s2)])
print (s)
0     3
1     5
2     7
3    10
4    14
5    17
6    19
7    23
8    25
9    27
dtype: int64
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • 1
    Tested this on broader range of dates, to make sure that there no weird timestamp conversions from pandas to numpy going on that could mess things up. Works perfectly, thanks. – user3820991 May 23 '19 at 11:59
0
from xone import calendar
def business_dates(start, end):
    us_cal = calendar.USTradingCalendar()
    kw = dict(start=start, end=end)
    return pd.bdate_range(**kw).drop(us_cal.holidays(**kw))

In [1]: business_dates(start='2018-12-20', end='2018-12-31')
Out[1]: DatetimeIndex(['2018-12-20', '2018-12-21', '2018-12-24', '2018-12-26',
                       '2018-12-27', '2018-12-28', '2018-12-31'],
                      dtype='datetime64[ns]', freq=None)
source Get business days between start and end date using pandas
vrana95
  • 511
  • 2
  • 10
0
#create dataframes with the dates
df=pd.DataFrame({'dates':pd.date_range(start='05/01/2019',end='05/31/2019')})
#check if the dates are in business days
df[df['dates'].isin(pd.bdate_range(df['dates'].get(0), df['dates'].get(len(df)-1)))]

out[]:
0   2019-05-01
1   2019-05-02
2   2019-05-03
5   2019-05-06
6   2019-05-07
7   2019-05-08
8   2019-05-09
9   2019-05-10
12  2019-05-13
13  2019-05-14
14  2019-05-15
15  2019-05-16
16  2019-05-17
19  2019-05-20
20  2019-05-21
21  2019-05-22
22  2019-05-23
23  2019-05-24
26  2019-05-27
27  2019-05-28
28  2019-05-29
29  2019-05-30
30  2019-05-31
Pyd
  • 6,017
  • 18
  • 52
  • 109