elegant way of convert a numpy array containing datetime.timedelta into seconds in python 2.7

numpy has its own datetime and timedelta formats. Just use them ;).

Set-up for example:

import datetime
import numpy

times = numpy.array([datetime.timedelta(0, 1, 36000)])

Code:

times.astype("timedelta64[ms]").astype(int) / 1000
#>>> array([ 1.036])

Since people don't seem to realise that this is the best solution, here are some timings of a timedelta64 array vs a datetime.datetime array:

SETUP="
import datetime
import numpy

times = numpy.array([datetime.timedelta(0, 1, 36000)] * 100000)
numpy_times = times.astype('timedelta64[ms]')
"

python -m timeit -s "$SETUP" "numpy_times.astype(int) / 1000"
python -m timeit -s "$SETUP" "numpy.vectorize(lambda x: x.total_seconds())(times)"
python -m timeit -s "$SETUP" "[delta.total_seconds() for delta in times]"

Results:

100 loops, best of 3: 4.54 msec per loop
10 loops, best of 3: 99.5 msec per loop
10 loops, best of 3: 67.1 msec per loop

The initial translation will take about two times as much time as the vectorized expression, but each operation from then-on into perpetuity on that timedelta array will be about 20 times faster.


If you're never going to use those timedeltas again, consider asking yourself why you ever made the deltas (as opposed to timedelta64s) in the first place, and then use the numpy.vectorize expression. It's less native but for some reason it's faster.


import numpy as np

helper = np.vectorize(lambda x: x.total_seconds())
dt_sec = helper(dt)