Search code examples
pythondatetimemeanmedianmode

Time calculations, mean , median, mode


( Name Gun_time Net_time Pace
John 28:48:00 28:47:00 4:38:00
George 29:11:00 29:10:00 4:42:00
Mike 29:38:00 29:37:00 4:46:00
Sarah 29:46:00 29:46:00 4:48:00
Roy 30:31:00 30:30:00 4:55:00

Q1. How can I add another column stating difference between Gun_time and Net_time? Q2. How will I calculate the mean for Gun_time and Net_time. Please help!

I have tried doing the following but it doesn't work

df['Difference'] = df['Gun_time'] - df['Net_time']

for mean value I tried df['Gun_time'].mean

but it doesn't work either, please help!

Q.3 What if we have times in 28:48 (minutes and seconds) format and not 28:48:00 the function gives out a value error.

ValueError: expected hh:mm:ss format


Solution

  • Convert your columns to dtype timedelta, e.g. like

    for col in ("Gun_time", "Net_time", "Pace"):
        df[col] = pd.to_timedelta(df[col])
    

    Now you can do calculations like

    df['Gun_time'].mean()
    # Timedelta('1 days 05:34:48')  
    

    or

    df['Difference'] = df['Gun_time'] - df['Net_time']
    
    #df['Difference']
    # 0   0 days 00:01:00
    # 1   0 days 00:01:00
    # 2   0 days 00:01:00
    # 3   0 days 00:00:00
    # 4   0 days 00:01:00
    # Name: Difference, dtype: timedelta64[ns]
    

    If you need nicer output to string, you can use

    def timedeltaToString(td):
        hours, remainder = divmod(td.total_seconds(), 3600)
        minutes, seconds = divmod(remainder, 60)
        return f"{int(hours):02d}:{int(minutes):02d}:{int(seconds):02d}"
    
    
    df['diffString'] = df['Difference'].apply(timedeltaToString)
    
    # df['diffString']
    # 0    00:01:00
    # 1    00:01:00
    # 2    00:01:00
    # 3    00:00:00
    # 4    00:01:00
    #Name: diffString, dtype: object
    

    See also Format timedelta to string.