Search code examples
pythondatetimedata-extraction

subtraction of previous time value from the next time value in python


I got this time array and I need to subtract the previous value from the next value. Like line 1-line 2 and line 3- line 4. I have been battling with this for days now, the problem now is how I will make the subtraction.

Here is the data:

2017-12-21T14:49:17.518Z
2017-12-21T14:50:49.723Z
2017-12-21T14:50:54.028Z
2017-12-21T14:50:54.343Z
2017-12-21T14:50:59.084Z
2017-12-21T14:50:59.399Z
2017-12-21T14:51:04.142Z
2017-12-21T14:51:04.457Z
2017-12-21T14:51:09.204Z
2017-12-21T14:51:09.521Z
2017-12-21T14:51:14.261Z
2017-12-21T14:51:14.579Z
2017-12-21T14:51:19.326Z
2017-12-21T14:51:19.635Z
2017-12-21T14:51:24.376Z
2017-12-21T14:51:24.691Z
2017-12-21T14:51:29.435Z
2017-12-21T14:51:29.750Z
2017-12-21T14:51:34.498Z
2017-12-21T14:51:34.813Z

I need to subtract the second from first, third from fourth, fifth from the sixth, and so on. Then get the result together in anther array and add them together.


Solution

  • Here is a way to do it with the datetime library and the strptime function:

    From your previous question, it seems like your times are a list of strings.

    times = [
        '2017-12-21T14:49:17.518Z',
        '2017-12-21T14:50:49.723Z',
        '2017-12-21T14:50:54.028Z',
        '2017-12-21T14:50:54.343Z',
        '2017-12-21T14:50:59.084Z',
        '2017-12-21T14:50:59.399Z',
        '2017-12-21T14:51:04.142Z',
        '2017-12-21T14:51:04.457Z',
        '2017-12-21T14:51:09.204Z',
        '2017-12-21T14:51:09.521Z',
        '2017-12-21T14:51:14.261Z',
        '2017-12-21T14:51:14.579Z',
        '2017-12-21T14:51:19.326Z',
        '2017-12-21T14:51:19.635Z',
        '2017-12-21T14:51:24.376Z',
        '2017-12-21T14:51:24.691Z',
        '2017-12-21T14:51:29.435Z',
        '2017-12-21T14:51:29.750Z',
        '2017-12-21T14:51:34.498Z',
        '2017-12-21T14:51:34.813Z'
    ]
    

    Convert them to datetime objects using strptime.The map() function applies a function to each element in an iterable.

    times_converted = map(
        lambda x: datetime.datetime.strptime(x, '%Y-%m-%dT%H:%M:%S.%fZ'), 
        times
    )
    

    The second argument to strptime above is the format string, which defines how the conversion should happen.

    Now you can subtract consecutive times and use the total_seconds() method of datetime.timedelta to get the difference as desired:

    diffs = [
        (b-a).total_seconds() for a, b in zip(times_converted[::2], times_converted[1::2])
    ]
    #[92.205, 0.315, 0.315, 0.315, 0.317, 0.318, 0.309, 0.315, 0.315, 0.315]
    

    I used zip() to get pairs of times from the list to subtract. The notation [::2] means take every other item in the list, starting at index 0. Likewise [1::2] means take every other item in the list, starting at index 1. More on python's slice notation here.

    If you're not comfortable with list comprehensions and zip(), the above code can also be written as single for loop:

    diffs = []
    for i in range(0,len(times), 2):
        diffs.append((times_converted[i+1]-times_converted[i]).total_seconds())
    

    More on zip()

    The zip() function takes two iterables and returns pairs of tuples. For instance, consider the following example:

    # suppose you had two lists
    X = ['A', 'B', 'C']
    Y = ['X', 'Y', 'Z']
    print(zip(X, Y))
    #[('A', 'X'), ('B', 'Y'), ('C', 'Z')]
    

    So essentially it takes one item from the first list and one item from the second list and returns this as a tuple.