Search code examples
pythonpandasdatetimebusiness-logic

calculate business days including custom holidays for two dates columns in python


I am trying to get the business days between two date columns by adding a new date_difference column.

What have I tried?

bd = workday(ll.date_done,ll.order_date, cal)
bd = networkdays(ll.date_done,ll.order_date, cal)
km = np.busday_count(dd.date(), od.date(), holidays = cal)

An error I am getting.

'Series' object has no attribute 'days'

What I am trying to do.

# Table Name: ll
order_date  date_done
2017-04-09  2017-04-16
2017-04-09  2017-04-18
2017-04-10  2017-04-20

What I am expecting:

order_date  date_done      Date_Difference
2017-04-09  2017-04-16        4
2017-04-09  2017-04-18        7
2017-04-10  2017-04-20        6

What Calculation I am doing?

I have holidays list excluding which I am trying to get the business days difference from two dates. The above date_difference calculation is just dummy numbers.


Solution

  • You can use numpy.bus_daycount() if you pass it the correct types as:

    Code:

    df['date_diff'] = np.busday_count(
        df.order_date.values.astype('datetime64[D]'),
        df.date_done.values.astype('datetime64[D]'))
    

    Test Code:

    import pandas as pd
    import numpy as np
    
    from io import StringIO
    df = pd.read_fwf(StringIO(u"""
        order_date  date_done
        2017-04-09  2017-04-16
        2017-04-09  2017-04-18
        2017-04-10  2017-04-20"""), header=1)
    df.order_date = pd.to_datetime(df.order_date, infer_datetime_format=True)
    df.date_done = pd.to_datetime(df.date_done, infer_datetime_format=True)
    
    df['date_diff'] = np.busday_count(
        df.order_date.values.astype('datetime64[D]'),
        df.date_done.values.astype('datetime64[D]'))
    
    print(df)
    

    Results:

      order_date  date_done  date_diff
    0 2017-04-09 2017-04-16          5
    1 2017-04-09 2017-04-18          6
    2 2017-04-10 2017-04-20          8