Skip to content Skip to sidebar Skip to footer

Numpy Odd Behaviour Conversion To Datetime64 Dtype

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

Solution 1:

Reproducing 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, the string arguments get passed on to numpy.datetime64 and get parsed properly, exactly as you've described. In the second, it needs to perform an explicit coercion from |S8 as surmised. It turns out this is being considered, but currently explicitly isn't supported:

This didn't go in, because the datetime properties don't exist on the arrays after you convert them to datetime64, so there could be some unintuitive consequences from that. When Martin implemented the quaternion dtype, we discussed the possibility that dtypes could expose properties that show up on the array object, and if this were implemented I think the conversion and compatibility between python datetime and datetime64 could be made quite natural.

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.

Post a Comment for "Numpy Odd Behaviour Conversion To Datetime64 Dtype"