我创建了一个自定义日历:
holidays_list = [...] # list of all weekends and holidays for needed time period
class MyBusinessCalendar(AbstractHolidayCalendar):
start_date = datetime(2011, 1, 1)
end_date = datetime(2017, 12, 31)
rules = [
Holiday(name='Day Off', year=d.year, month=d.month, day=d.day) for d in holidays_list
]
cal = MyBusinessCalendar()
我知道发薪日是每个月的第 5 天和第 20 天,如果这些天是休息日,则为之前的工作日。所以我拿
bus_day = CustomBusinessDay(calendar=cal)
r = pd.date_range('2011-01-01', '2017-12-31', freq=bus_day)
如果是发薪日,我想从 r
计算每一天。我怎样才能得到这个?
发薪日列表(美式英语paydays)你定义为:
the 5th and the 20th days of each month or the previous business days if these ones are days off
要使用假期日历以编程方式生成发薪日列表,您可以生成每月 6 日和每月 21 日的列表:
dates = [date(year, month, 6) for month in range(1, 13)] +
[date(year, month, 21) for month in range(1, 13)]
然后获取前一个工作日,即offset=-1。我会用这个:
np.busday_offset(dates, -1, roll='forward', holidays=my_holidays)
tôi sử dụng numpy.busday_offset
而不是 Pandas 的东西来进行偏移的原因是它是矢量化的并且运行速度非常快,而 Pandas busday 偏移逻辑非常慢。如果日期的数量很少,那没关系。如果需要,您仍然可以使用 Pandas 生成假期列表。
Xin lưu ý rằngroll='forward'
是因为您希望逻辑是,如果 6 号是周末或节假日,则向前滚动到 7 号或 8 号,然后从那里抵消-1 个工作日即可获得发薪日。
Tôi là một lập trình viên xuất sắc, rất giỏi!