How to find the number of hours between two dates

2019-07-29 05:46发布

问题:

I'm trying to find a very clean method to calculate the number of hours between two dates excluding weekends and certain holidays.

What I found out is that the package BusinessHours (https://pypi.python.org/pypi/BusinessHours/1.01) can do this. However I did not find any instruction on how to use the package (the syntax actually) especially how to input the holidays. I found the original code of the package (https://github.com/dnel/BusinessHours/blob/master/BusinessHours.py) but still not so sure. I guess it could be something like this:

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])

Still, where can I input the holidays? And what if I do not want to exclude the non-office-hour, am I just adjust the worktiming to worktiming=[0,23]?

Anyone know how to use this package please tell me about it. I appreciate it.

P/s: I knew a command in numpy to get the number of business days between 2 dates (busday_count) but there is no command to get the result in hours. Any other commands in pandas or numpy that can fulfill the task are welcomed too. Thank you

回答1:

The most current pip install of this package 1.2 has an error in line 51 with "extraday" which needs to be changed to "extradays" .

I too have been scouring the internet for some workable code to calculate business hours and business days. This package had a little bit of tweeking but works just fine when you get it up and running.

This is what I have in my notebook:

#import BusinessHours
from BusinessHours import BusinessHours as bh
import numpy as np
import pandas as pd
from pandas import Series, DataFrame

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

This was also in the source code:

'''
holidayfile - A file consisting of the predetermined office holidays. 
Each date starts in a new line and currently must only be in the format 
dd-mm-yyyy
'''

Hope this helps



回答2:

Try out this package called business-hour in PyPi

Example Code

from business_duration import businessDuration
import pandas as pd
from datetime import time,datetime
import holidays as pyholidays

startdate = pd.to_datetime('2017-01-01 00:00:00')
enddate = pd.to_datetime('2017-01-22 12:00:00')

holidaylist = pyholidays.Australia()
unit='hour'
#By default Saturday and Sunday are excluded

print(businessDuration(startdate,enddate,holidaylist=holidaylist,unit=unit))
 Output: 335.99611

holidaylist:
{datetime.date(2017, 1, 1): "New Year's Day",
 datetime.date(2017, 1, 2): "New Year's Day (Observed)",
 datetime.date(2017, 1, 26): 'Australia Day',
 datetime.date(2017, 3, 6): 'Canberra Day',
 datetime.date(2017, 4, 14): 'Good Friday',
 datetime.date(2017, 4, 15): 'Easter Saturday',
 datetime.date(2017, 4, 17): 'Easter Monday',
 datetime.date(2017, 4, 25): 'Anzac Day',
 datetime.date(2017, 6, 12): "Queen's Birthday",
 datetime.date(2017, 9, 26): 'Family & Community Day',
 datetime.date(2017, 10, 2): 'Labour Day',
 datetime.date(2017, 12, 25): 'Christmas Day',
 datetime.date(2017, 12, 26): 'Boxing Day'}