gpt4 book ai didi

pandas - 如何在 Python 中查找不包括周末和某些假期的两个日期之间的小时数?营业时间套餐

转载 作者:行者123 更新时间:2023-12-03 23:57:24 25 4
gpt4 key购买 nike

我试图找到一种非常干净的方法来计算 的数量小时 两个日期之间,不包括 周末 某些假期 .

我发现 BusinessHours ( https://pypi.python.org/pypi/BusinessHours/1.01 ) 包可以做到这一点。但是我没有找到任何关于如何使用包(实际上是语法)的说明,尤其是如何输入假期。
我找到了包的原始代码( https://github.com/dnel/BusinessHours/blob/master/BusinessHours.py ),但仍然不太确定。
我想它可能是这样的:

date1 = pd.to_datetime('2017-01-01 00:00:00')
date2 = pd.to_datetime('2017-01-22 12:00:00')
import BusinessHour
gethours(date1, date2, worktiming=[8, 17], weekends=[6, 7])

不过,我在哪里可以输入假期?如果我这样做会怎样 不是 想排除非办公时间,难道我只是调整了 worktimingworktiming=[0,23] ?

谁知道如何使用这个包,请告诉我。我很感激。

P/s:我知道 numpy 中有一个命令可以获取两个日期之间的工作日数( busday_count ),但是没有命令可以在 中获取结果小时 . pandas 或 numpy 中可以完成任务的任何其他命令也受到欢迎。
谢谢

最佳答案

重用来自那里的源代码,我组装了这段似乎有效的代码(适用于英国假期),但我很想就如何改进它发表评论。
我知道它不是特别优雅,但可能会对某人有所帮助。
顺便说一句,我想找到一种方法将假日图书馆中的日历插入到这个日历中。

无论如何,目前它不需要很多库,只需要 Pandas 和日期时间,这可能是一个加分项。


import pandas as pd
import datetime
from pandas.tseries.offsets import CDay
from pandas.tseries.holiday import (
AbstractHolidayCalendar, DateOffset, EasterMonday,
GoodFriday, Holiday, MO,
next_monday, next_monday_or_tuesday)

# This function will calculate the number of working minutes by first
# generating a time series of business days. Then it will calculate the
# precise working minutes for the start and end date, and use the total
# working hours for each day in-between.

def count_mins(starttime,endtime, bus_day_series, bus_start_time,bus_end_time):
mins_in_working_day=(bus_end_time-bus_start_time)*60

# now we are going to take the series of business days (pre-calculated)
# and sub select the period provided as argument of the function
# we could do the calculation of that "calendar" in the function itself
# but to improve performance, we calculate it separately and then we c
# call the function with that series as argument, provided the dates
# fall within the calculated range, of course
days = bus_day_series[starttime.date():endtime.date()]

daycount = len(days)
if len(days)==0:
return 0
else:
first_day_start = days[0].replace(hour=bus_start_time, minute=0)
first_day_end = days[0].replace(hour=bus_end_time, minute=0)
first_period_start = max(first_day_start, starttime)
first_period_end = min(first_day_end, endtime)
if first_period_end<=first_period_start:
first_day_mins=0
else:
first_day_sec=first_period_end - first_period_start
first_day_mins=first_day_sec.seconds/60
if daycount == 1:
return first_day_mins
else:
last_period_start = days[-1].replace(hour=bus_start_time, minute=0)
#we know the last day will always start in the bus_start_time

last_day_end = days[-1].replace(hour=bus_end_time, minute=0)
last_period_end = min(last_day_end, endtime)
if last_period_end<=last_period_start:
last_day_mins=0
else:
last_day_sec=last_period_end - last_period_start
last_day_mins=last_day_sec.seconds/60
middle_days_mins=0
if daycount>2:
middle_days_mins=(daycount-2)*mins_in_working_day
return first_day_mins + last_day_mins + middle_days_mins


# Calculates the date series with all the business days
# of the period we are interested on
class EnglandAndWalesHolidayCalendar(AbstractHolidayCalendar):
rules = [
Holiday('New Years Day', month=1, day=1, observance=next_monday),
GoodFriday,
EasterMonday,
Holiday('Early May bank holiday',
month=5, day=1, offset=DateOffset(weekday=MO(1))),
Holiday('Spring bank holiday',
month=5, day=31, offset=DateOffset(weekday=MO(-1))),
Holiday('Summer bank holiday',
month=8, day=31, offset=DateOffset(weekday=MO(-1))),
Holiday('Christmas Day', month=12, day=25, observance=next_monday),
Holiday('Boxing Day',
month=12, day=26, observance=next_monday_or_tuesday)
]

# From this point its how we use the function



# Here we hardcode a start/end date to create the list of business days
cal = EnglandAndWalesHolidayCalendar()
dayindex = pd.bdate_range(datetime.date(2019,1,1),datetime.date.today(),freq=CDay(calendar=cal))
day_series = dayindex.to_series()


# Convenience function to simplify how we call the main function
# It will take a pre calculated day_series.
def bus_hr(ts_start, ts_end, day_series ):
BUS_START=8
BUS_END=20
minutes = count_mins(ts_start, ts_end, day_series, BUS_START, BUS_END)
return int(round(minutes/60,0))


#A set of checks that the function is working properly
assert bus_hr( pd.Timestamp(2019,9,30,6,1,0) , pd.Timestamp(2019,10,1,9,0,0),day_series) == 13
assert bus_hr( pd.Timestamp(2019,10,3,10,30,0) , pd.Timestamp(2019,10,3,23,30,0),day_series)==10
assert bus_hr( pd.Timestamp(2019,8,25,10,30,0) , pd.Timestamp(2019,8,27,10,0,0),day_series) ==2
assert bus_hr( pd.Timestamp(2019,12,25,8,0,0) , pd.Timestamp(2019,12,25,17,0,0),day_series) ==0
assert bus_hr( pd.Timestamp(2019,12,26,8,0,0) , pd.Timestamp(2019,12,26,17,0,0),day_series) ==0
assert bus_hr( pd.Timestamp(2019,12,27,8,0,0) , pd.Timestamp(2019,12,27,17,0,0),day_series) ==9
assert bus_hr( pd.Timestamp(2019,6,24,5,10,44) , pd.Timestamp(2019,6,24,7,39,17),day_series)==0
assert bus_hr( pd.Timestamp(2019,6,24,5,10,44) , pd.Timestamp(2019,6,24,8,29,17),day_series)==0
assert bus_hr( pd.Timestamp(2019,6,24,5,10,44) , pd.Timestamp(2019,6,24,10,0,0),day_series)==2
assert bus_hr(pd.Timestamp(2019,4,30,21,19,0) , pd.Timestamp(2019,5,1,16,17,56),day_series)==8
assert bus_hr(pd.Timestamp(2019,4,30,21,19,0) , pd.Timestamp(2019,5,1,20,17,56),day_series)==12

关于pandas - 如何在 Python 中查找不包括周末和某些假期的两个日期之间的小时数?营业时间套餐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42409274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com