0

I have the following dataframe with a couple of columns with the beginning and the end of the dates' range I'd like to check the business days between:

In [139]: df.head()
Out[139]: 
         Num  Material  Kilos        Sold      Date   Year  Week_Number  \
0    5881999   1020252    629     1434120 2019-06-14  2019           24   
1   94623000   1022746    598     1572740 2019-06-12  2019           24   
2   77274650   1022470    920     1684520 2019-06-25  2019           26   
3   94623000   1022643   1215     2563650 2019-01-23  2019            4   
40  94623000   1021478    581     1528030 2018-10-23  2018           43   

    Week_init   Week_end  
0  2019-06-10 2019-06-15  
1  2019-06-10 2019-06-15  
2  2019-06-24 2019-06-29  
3  2019-01-21 2019-01-26  
40 2018-10-22 2018-10-27 

However, when I try to use the np.busday_count funcion as follows:

df["b_days"] = np.busday_count(df['Week_init'], df['Week_fin'], 
  weekmask=bday_custom.weekmask, holidays=bday_custom.holidays)

I get the following error:

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

Looks like the function is complaining about the Week_init and Week_end dtypes, but I've tried changing it's type without success as follows:

df['Week_init'] = df['Week_init'].values.astype('datetime64[D]')

However, dtype keeps as:

In [140]: df['Week_init'].dtype
Out[140]: dtype('<M8[ns]')

Any help please?

Arduino
  • 373
  • 1
  • 3
  • 21
  • Add `dt.date` for example `np.busday_count(df['Week_init'].dt.date, df['Week_end'].dt.date)` – Michael Gardner Nov 11 '19 at 14:22
  • Thanks Michael, tried that but the new type 'object' returns the following error: `TypeError: Cannot cast array data from dtype('O') to dtype(' – Arduino Nov 11 '19 at 15:36
  • I think some of your dates are string representations. Convert them to dates with `pd.to_datetime` before passing them into `np.busday_count` – Michael Gardner Nov 11 '19 at 15:51
  • Did that but the dates were already datetime elements, so still facing the same issue. – Arduino Nov 11 '19 at 16:51
  • 1
    https://stackoverflow.com/questions/31841487/pandas-number-of-business-days-between-a-datetimeindex-and-a-timestamp – Michael Gardner Nov 11 '19 at 19:52
  • There it was! The correct format for the dates was `pd.to_datetime(df['Week_init']).values.astype('datetime64[D]')`, found in that topic. Thanks! – Arduino Nov 12 '19 at 00:17

0 Answers0