[Numpy-discussion] numpy datetime64 NaT string conversion bug & patch

Charles G. Waldman charles at crunch.io
Wed Nov 27 15:51:04 EST 2013


If you convert an array of strings to datetime64s and 'NaT' (or one of
its variants) appears in the string, all subsequent values are
rendered as NaT:

(this is in 1.7.1 but the problem is present in current dev version as well)

>>> import numpy as np
>>> a = np.array(['2010', 'nat', '2030'])
>>> a.astype(np.datetime64)
array(['2010', 'NaT', 'NaT'], dtype='datetime64[Y]')

The fix is to re-initalize 'dt' inside the loop in
_strided_to_strided_string_to_datetime
(patch attached)



Correct behavior (with patch):
>>> import numpy as np
>>> a=np.array(['2010', 'nat', '2020'])
>>> a.astype(np.datetime64)
array(['2010', 'NaT', '2020'], dtype='datetime64[Y]')
>>>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: datetime64-nat-bug.patch
Type: text/x-patch
Size: 758 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20131127/382b8054/attachment.bin>


More information about the NumPy-Discussion mailing list