python計算一段時間內的工作日(除週末、除中國節假日、加調休)和所有日期

python計算一段時間內的工作日(除週末)

’計算兩個日期之間有幾個工作日(除週末)、有幾天(不除週末)都可以用python裏pandas的函數實現。

1. 計算有幾個工作日:

(1)bdate_range(start_day, end_day,freq='b')

start_day: 開始日期,日期格式可以兼容多種格式:2020-01-1或20200101或者1/1/2020!!!可以說是很完美了。

end_day:結束日期

freq:頻率,bdate_range函數不帶該參數時默認是'b',即工作日。計算工作日時,這個freq固定爲B或b或者不帶這個參數可以

以下是不同格式的實現:

# -*- coding: utf-8 -*-
"""
Created on Thu Mar 19 20:16:03 2020

@author: DELL
"""

def weekday_1():
    import pandas as pd
    e = pd.bdate_range('20200319', '20200326',freq='b')
    #bdate_range 6 
    #date_range 8
    print (e)
    minutes = len(e)  
    print(minutes)
    
weekday_1()

def weekday_2():
    import pandas as pd
    e = pd.bdate_range('19/3/2020', '26/03/2020',freq='B')
    #bdate_range 6 
    #date_range 8
    print (e)
    minutes = len(e)  
    print(minutes)
    
weekday_2()

def weekday_3():
    import pandas as pd
    e = pd.bdate_range('2020-03-19', '2020-03-26')
    #bdate_range 6 
    #date_range 8
    print (e)
    minutes = len(e)  
    print(minutes)
    
weekday_3()

運行結果

runfile('C:/Users/DELL/Desktop/untitled0.py', wdir='C:/Users/DELL/Desktop')
DatetimeIndex(['2020-03-19', '2020-03-20', '2020-03-23', '2020-03-24',
               '2020-03-25', '2020-03-26'],
              dtype='datetime64[ns]', freq='B')
6
DatetimeIndex(['2020-03-19', '2020-03-20', '2020-03-23', '2020-03-24',
               '2020-03-25', '2020-03-26'],
              dtype='datetime64[ns]', freq='B')
6
DatetimeIndex(['2020-03-19', '2020-03-20', '2020-03-23', '2020-03-24',
               '2020-03-25', '2020-03-26'],
              dtype='datetime64[ns]', freq='B')
6

(2)date_range(start_day, end_day,freq='b')

date_range 默認的freq是'd'就是日期的意思,如果不帶參數計算出來就是所有的天數,所有如果要用date_range計算工作日,必須要帶freq='b'的參數,其他開始日期和結束日期的格式跟bdate_range一樣。

# -*- coding: utf-8 -*-
"""
Created on Thu Mar 19 20:16:03 2020

@author: DELL
"""

def weekday_4():
    import pandas as pd
    e = pd.date_range('20200319', '20200326',freq='B')
    #bdate_range 6 
    #date_range 8
    print (e)
    minutes = len(e)  
    print(minutes)
    
weekday_4()


runfile('C:/Users/DELL/Desktop/untitled0.py', wdir='C:/Users/DELL/Desktop')
DatetimeIndex(['2020-03-19', '2020-03-20', '2020-03-23', '2020-03-24',
               '2020-03-25', '2020-03-26'],
              dtype='datetime64[ns]', freq='B')
6

2. 計算間隔的所有日期數(包含週末):

bdate_range(start_day, end_day,freq='d')

或 date_range(start_day, end_day)

# -*- coding: utf-8 -*-
"""
Created on Thu Mar 19 20:16:03 2020

@author: DELL
"""

def ALLday_1():
    import pandas as pd
    e = pd.bdate_range('20200319', '20200326',freq='d')
    #bdate_range 6 
    #date_range 8
    print (e)
    minutes = len(e)  
    print(minutes)
    
ALLday_1()

def ALLday_2():
    import pandas as pd
    e = pd.date_range('19/3/2020', '26/03/2020',freq='d')
    #bdate_range 6 
    #date_range 8
    print (e)
    minutes = len(e)  
    print(minutes)
    
ALLday_2()

def ALLday_3():
    import pandas as pd
    e = pd.date_range('2020-03-19', '2020-03-26')
    #bdate_range 6 
    #date_range 8
    print (e)
    minutes = len(e)  
    print(minutes)
    
ALLday_3()

運行結果:

runfile('C:/Users/DELL/Desktop/untitled0.py', wdir='C:/Users/DELL/Desktop')
DatetimeIndex(['2020-03-19', '2020-03-20', '2020-03-21', '2020-03-22',
               '2020-03-23', '2020-03-24', '2020-03-25', '2020-03-26'],
              dtype='datetime64[ns]', freq='D')
8
DatetimeIndex(['2020-03-19', '2020-03-20', '2020-03-21', '2020-03-22',
               '2020-03-23', '2020-03-24', '2020-03-25', '2020-03-26'],
              dtype='datetime64[ns]', freq='D')
8
DatetimeIndex(['2020-03-19', '2020-03-20', '2020-03-21', '2020-03-22',
               '2020-03-23', '2020-03-24', '2020-03-25', '2020-03-26'],
              dtype='datetime64[ns]', freq='D')
8

總結:bdate_range和date_range這兩個函數都可以計算工作日和所有日期,關鍵取決於freq參數。

(1)bdate_range 的freq參數默認是b,也就是businesstime工作日;如果要用bdate_range 計算所有日期必須限制freq是d。

(2)date_range的freq參數默認是d,也就是day所有日期;如果要用bdate_range 計算所有日期必須限制freq是b。

b和d都是跟day維度的,所以從3月19號到3月26號,共包含:19、20、21、22、23、24、25、26八天,其中工作日有6天。

 

3. 利用freq='min'計算所有日期:

bdate_range(start_day, end_day,freq='min')

min是按照分鐘維度來計算的,它是從start_day的00:00:00到end_day的00:00:00;其實end_day這一天從00:00:00到23:59:59是沒有計算的,所以用freq='min'計算出來是比d計算出來少一天的。

# -*- coding: utf-8 -*-
"""
Created on Thu Mar 19 20:16:03 2020

@author: DELL
"""

def ALLday_4():
    import pandas as pd
    e = pd.bdate_range('20200319', '20200326',freq='min')
    #bdate_range 6 
    #date_range 8
    print (e)
    minutes = len(e)  
    print(minutes//(60*24))
    
ALLday_4()

運行結果如下:

runfile('C:/Users/DELL/Desktop/untitled0.py', wdir='C:/Users/DELL/Desktop')
DatetimeIndex(['2020-03-19 00:00:00', '2020-03-19 00:01:00',
               '2020-03-19 00:02:00', '2020-03-19 00:03:00',
               '2020-03-19 00:04:00', '2020-03-19 00:05:00',
               '2020-03-19 00:06:00', '2020-03-19 00:07:00',
               '2020-03-19 00:08:00', '2020-03-19 00:09:00',
               ...
               '2020-03-25 23:51:00', '2020-03-25 23:52:00',
               '2020-03-25 23:53:00', '2020-03-25 23:54:00',
               '2020-03-25 23:55:00', '2020-03-25 23:56:00',
               '2020-03-25 23:57:00', '2020-03-25 23:58:00',
               '2020-03-25 23:59:00', '2020-03-26 00:00:00'],
              dtype='datetime64[ns]', length=10081, freq='T')
7

分鐘/60=小時

小時/24=天數

最後計算出來就是7天。

 

python計算一段時間內的工作日(除週末、除中國節假日加調休)

’計算兩個日期之間有幾個工作日(除週末、除假期加調休)可以用python裏pandas的函數實現。

1. 計算有兩個日期之間有幾個工作日:

只需要配置節假日和調休日,放在列表中就ok!

我們以2020年5月爲例,實現:計算5.1到5.9號工作日有幾天:應該是5.6號、5.7號、5.8號、5.9號四天

start_day = '2020-05-01'
end_day = '2020-05-09'
import pandas as pd
from pandas.tseries.offsets import CustomBusinessDay
def count_businessday(start_day,end_day):
    b = CustomBusinessDay(holidays=['2020-04-06','2020-05-01','2020-05-04','2020-05-05','2020-06-25','2020-10-01',
                                    '2020-10-05','2020-10-06','2020-10-07','2020-10-08'])
    bus_day = pd.date_range(start=start_day, end=end_day, freq=b)
    length = len(bus_day)

    extra_work_day = ['2020-04-26','2020-05-09','2020-06-28','2020-09-27','2020-10-10']
    extra_len = 0
    for i in extra_work_day:
        if i>=start_day and i<=end_day:
            extra_len = extra_len+1

    print(length+extra_len)

if __name__=='__main__':
    count_businessday(start_day,end_day)

 

致謝:多虧小黃同學堅持不懈的一鼓作氣的打破砂鍋問到底的工作態度! 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章