I have a numpy array called dt
. Each element is of type datetime.timedelta
. For example:
JavaScript
x
3
1
>>>dt[0]
2
datetime.timedelta(0, 1, 36000)
3
how can I convert dt
into the array dt_sec
which contains only seconds without looping? my current solution (which works, but I don’t like it) is:
JavaScript
1
4
1
dt_sec = zeros((len(dt),1))
2
for i in range(0,len(dt),1):
3
dt_sec[i] = dt[i].total_seconds()
4
I tried to use dt.total_seconds()
but of course it didn’t work. any idea on how to avoid this loop?
Thanks
Advertisement
Answer
JavaScript
1
5
1
import numpy as np
2
3
helper = np.vectorize(lambda x: x.total_seconds())
4
dt_sec = helper(dt)
5