Reproduction of your results:
>>> a = numpy.array([20090913, 20101020, 20110125]) >>> numpy.datetime64(a.astype("S8").tolist()) array([2009-09-13 00:00:00, 2010-10-20 00:00:00, 2011-01-25 00:00:00], dtype=datetime64[us]) >>> numpy.datetime64(a.astype("S8")) array([1970-01-01 00:00:20.090913, 1970-01-01 00:00:20.101020, 1970-01-01 00:00:20.110125], dtype=datetime64[us])
Here's the key:
>>> a.astype("S8").tolist() ['20090913', '20101020', '20110125'] >>> a.astype("S8") array(['20090913', '20101020', '20110125'], dtype='|S8')
In the first case, string arguments are passed to numpy.datetime64 and processed correctly, exactly as you described. In the second case, it is necessary to perform explicit coercion from |S8 , as expected. It turns out this is being considered, but is currently not explicitly supported :
This was not included, because datetime properties do not exist on arrays after converting them to datetime64, so there may be some unintuitive consequences of this. When Martin implemented quaternion dtype, we discussed the possibility that dtypes can expose the properties that are displayed on the array object, and if that was I think the conversion and compatibility between python datetime and datetime64 can be made quite natural.
The documentation has more examples of work constraints that you might want to consider, including from other time-based time formats. If you feel that the need for an explicit type of coercion is wrong, I would recommend reporting this to the numpy team and, if possible, submitting your own patch.
MrGomez
source share