Difference between 2 dates in terms of Trading Exchange working days

57 Views Asked by At

I am trying to create a column which is the difference between two date columns in terms of the number of working days of trading exchange. My data frame is very huge (about two million rows), and I have attempted the following approach.

import pandas_market_calendars as mcal
def tradeDiff_udf(startDt, endDt):
    bseCalendar = mcal.get_calendar('XBOM')
    inputdates =[]
    inputdates.append(startDt)
    inputdates.append(endDt)
    datesList = bseCalendar.valid_days(start_date=startDt, end_date=endDt)
    dts_list= [d.strftime('%Y-%m-%d') for d in datesList]
    #Excluding startDt,endDt (dateList has them if they are working dates)
    res = list(set(inputdates)^set(dts_list))
    return len(res)

vv = np.vectorize(tradeDiff_udf)
 Data_df['TradingDays_Diff'] = vv(Data_df["Previous_Date"], Data_df["date"])

Vectorized custom function is known for its good computation speed. But, this approach is taking very long to complete. I preferred this instead of lambda approach expecting good speed. Kindly correct me if I'm wrong. Please help me with this. Thanks!

0

There are 0 best solutions below