Search code examples
pythonpandasstring-to-datetime

Convert fractional seconds passed since onset to a date time index


For my football data analysis, to use the pandas between_time function, I need to convert a list of strings representing fractional seconds from measurement onset into the pandas date_time index. The time data looks as follows:

Time data

In order to achieve this I tried the following:

df['Time'] = df['Timestamp']*(1/freq)
df.index = pd.to_datetime(df['Time'], unit='s')

In which freq=600 and Timestamp is the frame number counting up from 0.

I was expecting the new index to show the following format:

  • %y%m%d-%h%m%s%f

But unfortunately, the to_datetime doesn't know how to handle my type of time data (namely counting up till 4750s after the start).

My question is, therefore, how do I convert my time sample data into a date_time index.


Solution

  • Based on this topic I now created the following function:

    def timeDelta2DateTime(self, time_delta_list):
        '''This method converts a list containing the time since measurement onset [seconds] into a
        list containing dateTime objects counting up from 00:00:00.
    
        Args:
            time_delta_list (list): List containing the times since the measurement has started.
    
        Returns:
            list: A list with the time in the DateTime format.
        '''
    
        ### Use divmod to convert seconds to m,h,s.ms ###
        s, fs = list(zip(*[divmod(item, 1) for item in time_delta_list]))
        m, s = list(zip(*[divmod(item, 60) for item in s]))
        h, m = list(zip(*[divmod(item, 60) for item in m]))
    
        ### Create DatTime list ###
        ms = [item*1000 for item in fs] # Convert fractional seconds to ms
        time_list_int = list(zip(*[list(map(int,h)), list(map(int,m)), list(map(int,s)), list(map(int,ms))])) # Combine h,m,s,ms in one list
    
        ### Return dateTime object list ###
        return [datetime(2018,1,1,item[0],item[1],item[2],item[3]) for item in time_list_int]
    

    As it seems to very slow feel free to suggest a better option.