Skip to content
Advertisement

Converting time to take away

Hiya so I have a data frame which has the time something occurs in one column and the time that it ends in the next column. I need to try and find the time difference between the two, but theyre both strings so it wont simply let me compare them, is there a way I can change them to ints (theyre in the format HH:MM:SS) I found a way to split them using .split (I’ve put what I did for the original time below, the I could do the same for the second column and work them out from there, but I was wondering if there was an easier way?
… TIA!

q = 0
for int in range(long):
    intel = df_data_bad_time1.loc[q,'Time']
    H_M_S = intel.split(':')
    df_data_bad_time1.loc[q,'Hours'] = H_M_S[0]
    df_data_bad_time1.loc[q,'Mins'] = H_M_S[1]
    df_data_bad_time1.loc[q,'Secs'] = H_M_S[2]
    q = q + 1
df_data_bad_time1['Hours'] = pd.to_numeric(df_data_bad_time1['Hours'], errors='coerce').astype('Int64')
df_data_bad_time1['Mins'] = pd.to_numeric(df_data_bad_time1['Mins'], errors='coerce').astype('Int64')
df_data_bad_time1['Secs'] = pd.to_numeric(df_data_bad_time1['Secs'], errors='coerce').astype('Int64')
df_data_bad_time1.head(15)

Advertisement

Answer

Here’s a simple function I wrote, you can take a look at it and tell me if you don’t understand anything: https://repl.it/@d4nieldev/subtract-time

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement