Calculate working time difference between dates
Question:
I need to calculate the average time between the arrival of a task and its completion. But if the task arrives on Friday, the time becomes too long due to the fact that the task is not executed on the weekend:
DT_APP_ENTERED IN_WORK
0 2023-02-17 54.80
1 2023-02-20 10.36
2 2023-02-21 10.68
3 2023-02-26 19.87
4 2023-02-27 11.35
5 2023-02-28 10.68
6 2023-03-01 10.91
7 2023-03-02 14.36
8 2023-03-03 36.46
How can I correctly add days to a datetime if weekday is friday?
I’ve tried to make a function:
def add_days(start_datetime):
start_datetime = month['DT_APP_ENTERED']
if start_datetime.dt.weekday == 4:
start_datetime + dt.timedelta(days=2)
else:
start_datetime
return (start_datetime)
But when I’ve applied it to a DF, there became an error: Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()
Answers:
I suppose you use pandas
. You can use pd.offsets.BDay
:
df['NEW_DATE'] = (df['DT_APP_ENTERED'].where(df['DT_APP_ENTERED'].dt.dayofweek==4)
.add(pd.offsets.BDay(1))
.fillna(df['DT_APP_ENTERED']))
print(df)
# Output
DT_APP_ENTERED IN_WORK NEW_DATE
0 2023-02-17 54.80 2023-02-20 # Friday -> Monday
1 2023-02-20 10.36 2023-02-20
2 2023-02-21 10.68 2023-02-21
3 2023-02-26 19.87 2023-02-26
4 2023-02-27 11.35 2023-02-27
5 2023-02-28 10.68 2023-02-28
6 2023-03-01 10.91 2023-03-01
7 2023-03-02 14.36 2023-03-02
8 2023-03-03 36.46 2023-03-06 # Friday -> Monday
If you prefer, the new day is Sunday, replace pd.offsets.BDay(1)
by pd.Timedelta('2D')
:
df['NEW_DATE'] = (df['DT_APP_ENTERED'].where(df['DT_APP_ENTERED'].dt.dayofweek==4)
.add(pd.Timedelta('2D'))
.fillna(df['DT_APP_ENTERED']))
print(df)
# Output
DT_APP_ENTERED IN_WORK NEW_DATE
0 2023-02-17 54.80 2023-02-19 # Friday -> Sunday
1 2023-02-20 10.36 2023-02-20
2 2023-02-21 10.68 2023-02-21
3 2023-02-26 19.87 2023-02-26
4 2023-02-27 11.35 2023-02-27
5 2023-02-28 10.68 2023-02-28
6 2023-03-01 10.91 2023-03-01
7 2023-03-02 14.36 2023-03-02
8 2023-03-03 36.46 2023-03-05 # Friday -> Sunday
I need to calculate the average time between the arrival of a task and its completion. But if the task arrives on Friday, the time becomes too long due to the fact that the task is not executed on the weekend:
DT_APP_ENTERED IN_WORK
0 2023-02-17 54.80
1 2023-02-20 10.36
2 2023-02-21 10.68
3 2023-02-26 19.87
4 2023-02-27 11.35
5 2023-02-28 10.68
6 2023-03-01 10.91
7 2023-03-02 14.36
8 2023-03-03 36.46
How can I correctly add days to a datetime if weekday is friday?
I’ve tried to make a function:
def add_days(start_datetime):
start_datetime = month['DT_APP_ENTERED']
if start_datetime.dt.weekday == 4:
start_datetime + dt.timedelta(days=2)
else:
start_datetime
return (start_datetime)
But when I’ve applied it to a DF, there became an error: Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()
I suppose you use pandas
. You can use pd.offsets.BDay
:
df['NEW_DATE'] = (df['DT_APP_ENTERED'].where(df['DT_APP_ENTERED'].dt.dayofweek==4)
.add(pd.offsets.BDay(1))
.fillna(df['DT_APP_ENTERED']))
print(df)
# Output
DT_APP_ENTERED IN_WORK NEW_DATE
0 2023-02-17 54.80 2023-02-20 # Friday -> Monday
1 2023-02-20 10.36 2023-02-20
2 2023-02-21 10.68 2023-02-21
3 2023-02-26 19.87 2023-02-26
4 2023-02-27 11.35 2023-02-27
5 2023-02-28 10.68 2023-02-28
6 2023-03-01 10.91 2023-03-01
7 2023-03-02 14.36 2023-03-02
8 2023-03-03 36.46 2023-03-06 # Friday -> Monday
If you prefer, the new day is Sunday, replace pd.offsets.BDay(1)
by pd.Timedelta('2D')
:
df['NEW_DATE'] = (df['DT_APP_ENTERED'].where(df['DT_APP_ENTERED'].dt.dayofweek==4)
.add(pd.Timedelta('2D'))
.fillna(df['DT_APP_ENTERED']))
print(df)
# Output
DT_APP_ENTERED IN_WORK NEW_DATE
0 2023-02-17 54.80 2023-02-19 # Friday -> Sunday
1 2023-02-20 10.36 2023-02-20
2 2023-02-21 10.68 2023-02-21
3 2023-02-26 19.87 2023-02-26
4 2023-02-27 11.35 2023-02-27
5 2023-02-28 10.68 2023-02-28
6 2023-03-01 10.91 2023-03-01
7 2023-03-02 14.36 2023-03-02
8 2023-03-03 36.46 2023-03-05 # Friday -> Sunday