The last error is telling us that np.datetime
objects cannot multiply. Addition has been defined - you can add n
timesteps to a date and get another date. But it doesn't make any sense to multiply a date.
In [1238]: x=np.array([1000],dtype='datetime64[s]')
In [1239]: x
Out[1239]: array(['1970-01-01T00:16:40'], dtype='datetime64[s]')
In [1240]: x[0]*3
...
TypeError: ufunc multiply cannot use operands with types dtype('<M8[s]') and dtype('int32')
So the simple way to generate a range of datetime objects is to add range of timesteps. Here, for example, I'm using 10 second increments
In [1241]: x[0]+np.arange(0,60,10)
Out[1241]:
array(['1970-01-01T00:16:40', '1970-01-01T00:16:50', '1970-01-01T00:17:00',
'1970-01-01T00:17:10', '1970-01-01T00:17:20', '1970-01-01T00:17:30'], dtype='datetime64[s]')
The error in linspace
is the result of it trying to multiply the start
by 1.
, as seen in the full error stack:
In [1244]: np.linspace(x[0],x[-1],10)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-1244-6e50603c0c4e> in <module>()
----> 1 np.linspace(x[0],x[-1],10)
/usr/lib/python3/dist-packages/numpy/core/function_base.py in linspace(start, stop, num, endpoint, retstep, dtype)
88
89 # Convert float/complex array scalars to float, gh-3504
---> 90 start = start * 1.
91 stop = stop * 1.
92
TypeError: ufunc multiply cannot use operands with types dtype('<M8[s]') and dtype('float64')
Despite the comment it looks like it's just converting ints to float. Anyways it wasn't written with datetime64
objects in mind.
user89161's
is the way to go if you want to use the linspace
syntax, otherwise you can just add the increments of your choosen size to the start date.
arange
works with these dates:
In [1256]: np.arange(x[0],x[0]+60,10)
Out[1256]:
array(['1970-01-01T00:16:40', '1970-01-01T00:16:50', '1970-01-01T00:17:00',
'1970-01-01T00:17:10', '1970-01-01T00:17:20', '1970-01-01T00:17:30'], dtype='datetime64[s]')