I need to calculate the difference between two pandas df columns which contain dates. I need the difference to be in days and not to contain weekends.
I know I can use
np.busday_count()
to achieve this. I also know that for this function to work correctly I may need to swap to the datatype to datetime[64].
My problem is that my data may contain NaT in either input column for some values due to the source of the data. np.nusday_count() does not like this.
Is there a way I can calculate the difference between two columns containing dates, in days, while excluding the weekends, not running the calculation/returning a blank for anywhere where either column 1 or 2 would be NaT and input the data into a new column in the dataframe. I've been trying unsuccessfully for hours.
Frustratingly, if I did not have to ignore weekends, this can all be done very simply by just doing
df['Difference in Days'] = df['Column 2] - df['Column 1']
For what I want to achieve the initial df would be like this
Column 1 Column 2
07/10/20 09/10/20
07/10/20 12/10/20
NaT 09/10/20
07/10/20 NaT
With the final df like this
Column 1 Column 2 Difference in Days
07/10/20 09/10/20 2
07/10/20 12/10/20 3
NaT 09/10/20 Blank
07/10/20 NaT Blank