I'm using Python 3.6.2.
I've learnt from this question how to convert between the standard datetime
type to np.datetime64
type, as follows.
dt = datetime.now()
print(dt)
print(np.datetime64(dt))
Output:
2017-12-19 17:20:12.743969
2017-12-19T17:20:12.743969
But I can't convert an iterable of standard datetime
objects into a Numpy array. The following code ...
np.fromiter([dt], dtype=np.datetime64)
... gives the following error.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-14-46e4618bda89> in <module>()
----> 1 np.fromiter([dt], dtype=np.datetime64)
TypeError: Cannot cast datetime.datetime object from metadata [us] to according to the rule 'same_kind'
However, using np.asarray()
works.
np.asarray([dt])
Output:
array([datetime.datetime(2017, 12, 19, 17, 20, 12, 743969)], dtype=object)
Might this be a bug with either np.fromiter()
or np.datetime64
?