小编典典

两天之间的差异(不包括周末)

python

我有一个代码,可以使用np.busdaycount计算除周末以外的日期差异,但是我需要在我无法获得的小时数内。

import datetime
import numpy as np


df.Inflow_date_time= [pandas.Timestamp('2019-07-22 21:11:26')]
df.End_date_time= [pandas.Timestamp('2019-08-02 11:44:47')]

df['Day'] = ([np.busday_count(b,a) for a, b in zip(df['End_date_time'].values.astype('datetime64[D]'),df['Inflow_date_time'].values.astype('datetime64[D]'))])

  Day
0  9

除周末外,我的工作时间为小时。喜欢

  Hours
0  254

问题

Inflow_date_time = 2019-08-01 23:22:46 End_date_time = 2019-08-05
17:43:51预期小时数42小时(1 + 24 + 17)

Inflow_date_time = 2019-08-03 23:22:46 End_date_time = 2019-08-05 17:43:51
预计工时17小时(0 + 0 + 17)

Inflow_date_time = 2019-08-01 23:22:46 End_date_time = 2019-08-05
17:43:51预期的小时数17小时(0 + 0 + 17)

Inflow_date_time = 2019-07-26 23:22:46 End_date_time = 2019-08-05 17:43:51
预期小时138小时(1 + 120 + 17)

Inflow_date_time = 2019-08-05 11:22:46 End_date_time = 2019-08-05 17:43:51
预期小时数6小时(0 + 0 + 6)

请提出建议。


阅读 163

收藏
2021-01-20

共1个答案

小编典典

想法是按日期逐层删除times的楼层日期时间,并获取开始日期+一天之间的工作日数,然后按hours3列(如果不是周末时间)numpy.busday_count创建hour1hour2按楼层按小时数的开始和结束时间列。最后汇总所有小时数列:

df = pd.DataFrame(columns=['Inflow_date_time','End_date_time', 'need'])
df.Inflow_date_time= [pd.Timestamp('2019-08-01 23:22:46'),
                      pd.Timestamp('2019-08-03 23:22:46'),
                      pd.Timestamp('2019-08-01 23:22:46'),
                      pd.Timestamp('2019-07-26 23:22:46'),
                      pd.Timestamp('2019-08-05 11:22:46')]
df.End_date_time= [pd.Timestamp('2019-08-05 17:43:51')] * 5
df.need = [42,17,41,138,6]

#print (df)

df["hours1"] = df["Inflow_date_time"].dt.ceil('d')
df["hours2"] =  df["End_date_time"].dt.floor('d')
one_day_mask = df["Inflow_date_time"].dt.floor('d') == df["hours2"]

df['hours3'] = [np.busday_count(b,a)*24 for a, b in zip(df['hours2'].dt.strftime('%Y-%m-%d'),
                                                        df['hours1'].dt.strftime('%Y-%m-%d'))]

mask1 = df['hours1'].dt.dayofweek < 5
hours1 = df['hours1']  - df['Inflow_date_time'].dt.floor('H')

df['hours1'] = np.where(mask1, hours1, np.nan) / np.timedelta64(1 ,'h')

mask2 = df['hours2'].dt.dayofweek < 5

df['hours2'] = (np.where(mask2, df['End_date_time'].dt.floor('H')-df['hours2'], np.nan) / 
                np.timedelta64(1 ,'h'))

df['date_diff'] = df['hours1'].fillna(0) + df['hours2'].fillna(0) + df['hours3']

one_day = (df['End_date_time'].dt.floor('H') - df['Inflow_date_time'].dt.floor('H')) / 
            np.timedelta64(1 ,'h')
df["date_diff"] = df["date_diff"].mask(one_day_mask, one_day)

print (df)
     Inflow_date_time       End_date_time  need  hours1  hours2  hours3  \
0 2019-08-01 23:22:46 2019-08-05 17:43:51    42     1.0    17.0      24   
1 2019-08-03 23:22:46 2019-08-05 17:43:51    17     NaN    17.0       0   
2 2019-08-01 23:22:46 2019-08-05 17:43:51    41     1.0    17.0      24   
3 2019-07-26 23:22:46 2019-08-05 17:43:51   138     NaN    17.0     120   
4 2019-08-05 11:22:46 2019-08-05 17:43:51     6    13.0    17.0     -24

   date_diff  
0       42.0  
1       17.0  
2       42.0  
3      137.0  
4        6.0
2021-01-20