Business hours between two Series of Timestamps excluding weekends AND holidays

后端 未结 1 1817
余生分开走
余生分开走 2021-01-15 04:45

I have a pandas DataFrame that looks like this (sample):

data = { 
    \'start\': [\'2018-10-29 18:48:46.697000\',
              \'2018-10-29 19:01:10.887000         


        
相关标签:
1条回答
  • 2021-01-15 05:34

    You should use CustomBusinessHour and pd.date_range instead of pd.bdate_range.

    The number of hours for your second row should be 145 because endtime is 09:31:39.967.

    us_bh = CustomBusinessHour(calendar=USFederalHolidayCalendar())
    df['count'] = df.apply(lambda x: len(pd.date_range(start=x.start, end=x.end, freq= us_bh)),axis=1)
    df['diff'] = df.apply(lambda x: pd.date_range(start=x.start, end=x.end, freq= us_bh),axis=1)
    print(df)
    
    
        start                     end                  count                                               diff
    0 2018-10-29 18:48:46.697 2018-10-31 17:56:38.830     16  DatetimeIndex(['2018-10-30 09:00:00', '2018-10...
    1 2018-10-29 19:01:10.887 2018-11-27 09:31:39.967    145  DatetimeIndex(['2018-10-30 09:00:00', '2018-10...
    2 2018-10-22 17:42:24.467 2018-11-28 18:33:35.243    200  DatetimeIndex(['2018-10-23 09:00:00', '2018-10...
    

    And diff columns start business hour will '2018-10-29 09:00:00' when you use pd.bdate_range.

    us_bh = CustomBusinessHour(calendar=USFederalHolidayCalendar())
    df['count'] = df.apply(lambda x: len(pd.bdate_range(start=x.start, end=x.end, freq= us_bh)),axis=1)
    df['diff'] = df.apply(lambda x: pd.bdate_range(start=x.start, end=x.end, freq= us_bh),axis=1)
    print(df)
    
                        start                     end  count                                               diff
    0 2018-10-29 18:48:46.697 2018-10-31 17:56:38.830     16  DatetimeIndex(['2018-10-29 09:00:00', '2018-10...
    1 2018-10-29 19:01:10.887 2018-11-27 09:31:39.967    152  DatetimeIndex(['2018-10-29 09:00:00', '2018-10...
    2 2018-10-22 17:42:24.467 2018-11-28 18:33:35.243    200  DatetimeIndex(['2018-10-22 09:00:00', '2018-10...
    
    0 讨论(0)
提交回复
热议问题