import numpy
a = numpy.array([20090913, 20101020, 20110125])
Can you explain why numpy.datetime64(a.astype("S8").tolist()) converts correctly but not numpy.datetime64(a.astype("S8"))? Why would a numpy function not accept numpy strings and only regular python strings? I am using Numpy 1.6.1 with Python 2.7.2 Windows 7.
Reproducing your results:
Here’s the key:
In the first case, the string arguments get passed on to
numpy.datetime64and get parsed properly, exactly as you’ve described. In the second, it needs to perform an explicit coercion from|S8as surmised. It turns out this is being considered, but currently explicitly isn’t supported:The documentation has more examples of working coercions you may wish to consider, including from other numpy time formats. If you feel the need for explicit type coercion is in error, I’d recommend reporting it to the numpy team and, if possible, submitting your own patch.