有比这更好/更直接的计算方法吗?
(2:$)
给出了一个pandas.tseries.index.DatetimeIndex
# 1. Set up the start and end date for which you want to calculate the
# number of business days excluding holidays.
start_date = '01JAN1986'
end_date = '31DEC1987'
start_date = datetime.datetime.strptime(start_date, '%d%b%Y')
end_date = datetime.datetime.strptime(end_date, '%d%b%Y')
# 2. Generate a list of holidays over this period
from pandas.tseries.holiday import USFederalHolidayCalendar
calendar = USFederalHolidayCalendar()
holidays = calendar.holidays(start_date, end_date)
holidays
但你需要一个numpy busday_count的列表
DatetimeIndex(['1986-01-01', '1986-01-20', '1986-02-17', '1986-05-26',
'1986-07-04', '1986-09-01', '1986-10-13', '1986-11-11',
'1986-11-27', '1986-12-25', '1987-01-01', '1987-01-19',
'1987-02-16', '1987-05-25', '1987-07-03', '1987-09-07',
'1987-10-12', '1987-11-11', '1987-11-26', '1987-12-25'],
dtype='datetime64[ns]', freq=None, tz=None)
然后,无论有没有假期,你都会得到:
holiday_date_list = holidays.date.tolist()
还有一些其他问题略有相似,但通常使用pandas系列或数据框(Get business days between start and end date using pandas,Counting the business days between two series)
答案 0 :(得分:1)
如果将创建的索引放在数据框中,则可以使用resample来填补空白。传递给.resample()
的偏移量可以包括工作日,甚至(自定义)日历:
from pandas.tseries.holiday import USFederalHolidayCalendar
C = pd.offsets.CustomBusinessDay(calendar=USFederalHolidayCalendar())
start_date = '01JAN1986'
end_date = '31DEC1987'
(
pd.DataFrame(index=pd.to_datetime([start_date, end_date]))
.resample(C, closed='right')
.asfreq()
.index
.size
) - 1
索引的大小-1然后为我们提供了天数。
答案 1 :(得分:0)
您的回答很好。该库看起来非常相似:
https://github.com/seatgeek/businesstime
,不需要熊猫数据框。 (我在这里看到了此信息:Is there a function in Python/Pandas to get business time Delta between two date times? )