How to find weekdays between dates using Pandas CDay?
I defined a Pandas CDay()
object to store holidays. How do I use it to calculate the number of working days between two dates?
I know np.busday_count
, but my calendar happens to be Pandas CDay
.
Solution
You can simply pass week masks and holidays directly from the CDay
calendar to np.busday_count
。 .
np.busday_count(start_date, end_date,
weekmask=bday_custom.weekmask, holidays=bday_custom.holidays)
Or (but certainly slower), you can use >pd.date_ range and pass your custom CDay
calendar as freq
.
pd.date_range(datetime(2017, 3, 5), datetime(2017, 3, 12), freq=bday_cust).size
This has the unfortunate side effect of creating an intermediate date range that uses only its size.
Example
Let’s set up a meaningless custom weekday calendar.
from pandas.tseries.offsets import CustomBusinessDay
weekmask = 'Mon Wed Fri Sat'
holidays = [datetime(2017, 3, 6), datetime(2017, 3, 11)]
bday_cust = CustomBusinessDay(holidays=holidays, weekmask=weekmask)
We’ve now made Mondays and Saturdays for the week of March 5-11 weekdays (and holidays) weekdays. Now looking at that particular date range, we can calculate the remaining working days (2).
>>> np.busday_count(datetime(2017, 3, 5), datetime(2017, 3, 12),
weekmask=bday_custom.weekmask,
holidays=bday_custom.holidays)
2
>>> pd.date_range(datetime(2017, 3, 5), datetime(2017, 3, 12), freq=bday_cust).size
2
Rough example benchmark
%timeit np.busday_count(datetime(2017, 3, 5), datetime(2017, 3, 12),
weekmask=bday_custom.weekmask,
holidays=bday_custom.holidays)
100000 loops, best of 3: <b>17.2 us per loop</b>
% timeit pd.date_range(datetime(2017, 3, 5), datetime(2017, 3, 12), freq=bday_cust).size
1000 loops, best of 3: <b>573 us per loop</b>