1

I have 2 columns in my pandas data frame, and I want to calculate the business dates between them.

Data:

 ID     On hold     Off Hold
 101    09/15/2017  09/16/2017
 102    NA          NA
 103    09/22/2017  09/26/2017
 104    10/12/2017  10/30/2017
 105    NA          NA
 106    08/05/2017  08/06/2017
 107    08/08/2017  08/03/2017
 108    NA          NA

I tried the below code using busday_count from numpy:

 df1['On hold'] = pd.to_datetime(df1['On hold'])
 df1['Off Hold'] = pd.to_datetime(df1['Off Hold'])
 np.busday_count(df1['On hold'].values.astype('datetime64[D]'),df1['Off Hold'].values.astype('datetime64[D]'))

also,

 np.where(pd.notnull(df1['On hold']),np.busday_count(df1['On hold'].values.astype('datetime64[D]'),
                                                df1['Off Hold'].values.astype('datetime64[D]')),0)

The error was :

   Cannot compute a business day count with a NaT (not-a-time) date

Any help will be appreciated :)

Megha John
  • 153
  • 1
  • 12

3 Answers3

1

Using the pd.bdate_range method:

pd.bdate_range(df['On Hold'], df['Off Hold'], freq = 'B')
ShreyasG
  • 766
  • 4
  • 11
1

You can try the below:

f = df1.dropna()
f['days'] = np.busday_count(pd.to_datetime(f['On hold']).values.astype('datetime64[D]'), \
            pd.to_datetime(f['Off hold']).values.astype('datetime64[D]'))

df1.merge(f[['ID', 'days']],on='ID', how='left')
Sachit Nagpal
  • 486
  • 4
  • 7
0

Try to drop NA first before you do bus day count. You can drop all the NA by using the following.

new_df = df.dropna()
Jonathan
  • 8,453
  • 9
  • 51
  • 74