python - How to calculate time between events in a pandas -
original question
i'm stuck on following problem. i'm trying figure out @ moments in time , how long vehicle situated @ factory. have excel sheet in events stored either delivery routes or maintenance events. ultimate goal obtain dataframe in vehicle registration number given corresponding arrival @ factory , time spend there(including maintenance actions). people interested, because want able schedule non-critical maintenance actions on vehicles.
an example of dataframe be:
registration routid date dep loc arr loc dep time arr time days 0 xc66 a58 20/may/17 home loc 10:54 21:56 0 1 xc66 a59 21/may/17 loc home 00:12 10:36 0 2 xc66 a345 21/may/17 home loc b 12:41 19:16 0 3 xc66 a346 21/may/17 loc b loc c 20:50 03:49 1 4 xc66 a347 22/may/17 loc c home 06:10 07:40 0 5 xc66 #m1 22/may/17 home home 10:51 13:00 0
i have created script in dates , times processed create correct datetime columns arrival , departure datetimes. maintenance periods: "dep loc" = home , "arr loc" = home following code used single out relevant lines:
df_home = df[df["dep loc"].isin(["home"])] df_home = df_home[df_home["arr loc"].isin(["home"])]
from here can subtract dates create duration column.
so far good. however, i'm stuck on using calculating other times. because there might intermediate stops, .shift() function not work amount of rows shift not-constant.
i have tried search on matter find shift solutions, or answers based in internal event times, not on time between events.
any guidance in right direction appreciated!
regards
attempt of solution
i have been stuck on question while now, shortly after posting question tried solution:
for idx, loc in enumerate(df["arr loc"]): if loc == "home": = ((idx2, obj) idx2, obj in enumerate(df["dep loc"]) if (obj == "home" , idx2 > idx)) idx_next = next(a) idx_next = idx_next[0] arrival_times = df["arr time"] departure_times = df["dep time"] duration = arrival_times[idx] - departure_times[idx_next]
here used next function find next occurrence of home starting location(i.e. time vehicle leaves base). subsequently subtract 2 dates find proper time difference.
it works small data set, not still entire dataset.
Comments
Post a Comment