I have two date column and I need to calculate Business day between them. My data frame contain around 10k rows. I have written code. To calculate business day but the challenge is it is taking more than 10 minutes to calculate. Is there any fast method?
Below is my code.
from datetime import datetime
df_merge['First analysed date'] = pd.to_datetime(df_merge['First analysed date'])
df_merge['Requested date'] = pd.to_datetime(df_merge['Requested date'])
# Custom business day calendar excluding weekends.
weekmask = 'Mon Tue Wed Thu Fri Sat'
#PERSONALIZE Holidays
exclude = [datetime(2023, 4, 4), datetime(2023, 4, 11), datetime(2023, 4, 18), datetime(2023, 4, 19)]
bday_us = pd.offsets.CustomBusinessDay(weekmask=weekmask, holidays=exclude)
# Define a function to compute the number of business days between two dates
def num_business_days(row):
dates = pd.date_range(start=row['First analysed date'], end=row['Requested date'], freq=bday_us)
return len(dates)
# Apply the function to each row of the dataframe and store the results in a new column
df_merge['Analyze to PRR'] = df_merge.apply(num_business_days, axis=1)