Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 以熊猫为单位计算工资天数_Python_Pandas_Time Series - Fatal编程技术网

Python 以熊猫为单位计算工资天数

Python 以熊猫为单位计算工资天数,python,pandas,time-series,Python,Pandas,Time Series,我已经创建了一个自定义日历: 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 = [ Hol

我已经创建了一个自定义日历:

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
计算每天的工资。如何获取此信息?

您将发薪日列表(美国英语中的发薪日)定义为:

每月的第5天和第20天,或之前的工作日(如果这些工作日是休息日)

要使用假日日历以编程方式生成发薪日列表,您可以每6个月和每21个月生成一次发薪日列表:

dates = [date(year, month, 6) for month in range(1, 13)] +
    [date(year, month, 21) for month in range(1, 13)]
然后获取上一个工作日,即偏移量=-1。我会用这个:

np.busday_offset(dates, -1, roll='forward', holidays=my_holidays)
我之所以使用
numpy.busday\u offset
而不是Pandas来进行偏移,是因为它是矢量化的,运行速度非常快,而Pandas-busday偏移逻辑非常慢。如果约会的次数很少,那就没关系了。如果需要,您仍然可以使用Pandas生成假期列表


请注意,
roll='forward'
是因为您希望逻辑是,如果第六个工作日是在周末或假日,您可以向前滚动到第七个或第八个工作日,然后从那里您可以抵消-1个工作日以获得发薪日。

休息日仅为
规则规定的休息日,也可以是星期日(可能是星期六)?所有周六和周日都包含在
假日列表中