Skip to content Skip to sidebar Skip to footer

Business Hours Between Two Series Of Timestamps Excluding Weekends And Holidays

I have a pandas DataFrame that looks like this (sample): data = { 'start': ['2018-10-29 18:48:46.697000', '2018-10-29 19:01:10.887000', '2018-10-22

Solution 1:

You should use CustomBusinessHour and pd.date_range instead of pd.bdate_range.

The number of hours for your second row should be 145 because endtime is 09:31:39.967.

us_bh = CustomBusinessHour(calendar=USFederalHolidayCalendar())
df['count'] = df.apply(lambdax: len(pd.date_range(start=x.start, end=x.end, freq= us_bh)),axis=1)
df['diff'] = df.apply(lambdax: pd.date_range(start=x.start, end=x.end, freq= us_bh),axis=1)
print(df)


    start                     end                  count                                               diff
02018-10-2918:48:46.6972018-10-3117:56:38.83016  DatetimeIndex(['2018-10-30 09:00:00', '2018-10...
1 2018-10-29 19:01:10.887 2018-11-27 09:31:39.967    145  DatetimeIndex(['2018-10-30 09:00:00', '2018-10...
22018-10-2217:42:24.4672018-11-2818:33:35.243200  DatetimeIndex(['2018-10-23 09:00:00', '2018-10...

And diff columns start business hour will '2018-10-29 09:00:00' when you use pd.bdate_range.

us_bh = CustomBusinessHour(calendar=USFederalHolidayCalendar())
df['count'] = df.apply(lambdax: len(pd.bdate_range(start=x.start, end=x.end, freq= us_bh)),axis=1)
df['diff'] = df.apply(lambdax: pd.bdate_range(start=x.start, end=x.end, freq= us_bh),axis=1)
print(df)

                    start                     end  count                                               diff
02018-10-2918:48:46.6972018-10-3117:56:38.83016  DatetimeIndex(['2018-10-29 09:00:00', '2018-10...
1 2018-10-29 19:01:10.887 2018-11-27 09:31:39.967    152  DatetimeIndex(['2018-10-29 09:00:00', '2018-10...
22018-10-2217:42:24.4672018-11-2818:33:35.243200  DatetimeIndex(['2018-10-22 09:00:00', '2018-10...

Post a Comment for "Business Hours Between Two Series Of Timestamps Excluding Weekends And Holidays"