1

I have a dataframe of dates with a DateTimeIndex like this:

pd.DataFrame(['01-20-2017']*len(pd.date_range('01-01-2017', '01-30-2017')), columns=['Dates'], index=pd.date_range('01-01-2017', '01-30-2017'))

I would like to add a column that counts the difference in BUSINESS DAYS between the dates in the Dates column and the DateTimeIndex.

I am having great difficulty trying to do this, when I originally thought this would be straight forward...

Panda_User
  • 169
  • 1
  • 3
  • 12

1 Answers1

1

use np.busday_count()

df = pd.DataFrame({'begin' : pd.date_range('01-01-2017', '01-30-2017'), 'end' :  ['01-20-2017']*len(pd.date_range('01-01-2017', '01-30-2017'))}) 
df['begin'] = pd.to_datetime(df['begin'])
df['end'] = pd.to_datetime(df['end'])
df['diff'] = df.apply(lambda row: np.busday_count(row['begin'], row['end']),axis=1)

    begin   end diff
0   2017-01-01  2017-01-20  14
1   2017-01-02  2017-01-20  14
2   2017-01-03  2017-01-20  13
3   2017-01-04  2017-01-20  12
4   2017-01-05  2017-01-20  11
Deb
  • 1,098
  • 10
  • 22
  • Good solution but might want to confirm this aligns with pandas' business day calendar (`pandas.tseries.offsets.BDay`). The default for the `holidays` parameter in `busday_count` is an empty list. – Brad Solomon Aug 21 '17 at 12:32