how to use (np.busday_count) with pandas.core.series.Series

Viewed 4982

I want to calculate the business days between two columns type pandas.core.series.Series

between Received date and complete date #business days only
I was trying to use np.busday_count(Received date,complete date,weekmask= "Fri Sat") but I was getting :

TypeError: Iterator operand 0 dtype could not be cast from dtype('M8[us]') to dtype('M8[D]') according to the rule 'safe'

Here is part of my dataframe:

     Received   Complete
0  2018-09-10 2018-09-25
1  2018-07-16 2018-08-13
2  2018-07-05 2018-07-11
3  2018-07-05 2018-07-23
4  2018-07-26 2018-08-14
5  2018-07-04 2018-08-28
6  2018-07-05 2018-07-10
7  2018-07-05 2018-07-10
8  2018-07-05 2018-07-22
9  2018-07-05 2018-07-22
10 2018-07-05 2018-07-19
1 Answers

As indicated in the error, convert to 'datetime64[D]' before using np.busday_count:

res = np.busday_count(df['Received'].values.astype('datetime64[D]'),
                      df['Complete'].values.astype('datetime64[D]'),
                      weekmask= "Fri Sat")

# array([ 4,  8,  2,  6,  6, 16,  2,  2,  6,  6,  4], dtype=int64)
Related