我目前正在使用
np.busday_count(day1, day2))
计算两个日期之间的工作日数。如何排除工作日的假期?
答案 0 :(得分:4)
自v0.14起,您可以使用假日日历
import pandas as pd
from pandas.tseries.holiday import USFederalHolidayCalendar
from pandas.tseries.offsets import CustomBusinessDay
day1 = '2010-01-01'
day2 = '2010-01-15'
us_bd = CustomBusinessDay(calendar=USFederalHolidayCalendar())
print(pd.DatetimeIndex(start=day1,end=day2, freq=us_bd))
输出:
DatetimeIndex(['2010-01-04', '2010-01-05', '2010-01-06', '2010-01-07',
'2010-01-08', '2010-01-11', '2010-01-12', '2010-01-13',
'2010-01-14', '2010-01-15'],
dtype='datetime64[ns]', freq='C')
要获得天数,请使用len
print(len(pd.DatetimeIndex(start=day1,end=day2, freq=us_bd)))
输出:
10