[Numpy-discussion] Dates and times and Datetime64 (again)

Stephan Hoyer shoyer at gmail.com
Sat Apr 19 00:50:38 EDT 2014


On Mon, Apr 14, 2014 at 11:59 AM, Chris Barker <chris.barker at noaa.gov>wrote:
>
> - datetime64 objects with high precision (e.g., ns) can't compare to
>> datetime objects.
>>
>
> That's a problem, but how do you think it should be handled? My thought is
> that it should round to microseconds, and then compare -- kind of like
> comparing float32 and float64...
>

I agree -- if the ns matter, you shouldn't be using datetime.datetime
objects.

Similarly, it's currently not possible to convert high precision datetime64
objects into datetimes. Worse, this doesn't even raise an error!

>>> from datetime import datetime
>>> import numpy as np
>>> np.datetime64('2000-01-01T00:00:00Z', 'us').astype(datetime)

datetime.datetime(2000, 1, 1, 0, 0)

>>> np.datetime64('2000-01-01T00:00:00Z', 'ns').astype(datetime)

946684800000000000L


Other inconsistent behavior:


>>> np.datetime64('2000', 'M')
numpy.datetime64('2000-01')
>>> np.datetime64('2000', 'D')
numpy.datetime64('2000-01-01')
>>> np.datetime64('2000', 's')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-67-bf5fc9a2985b> in <module>()
----> 1 np.datetime64('2000', 's')

TypeError: Cannot parse "2000" as unit 's' using casting rule 'same_kind'

More broadly, my recommendation is to look through the unit tests for
pandas' datetIme handling:
https://github.com/pydata/pandas/tree/master/pandas/tseries/tests

Not everything is relevant but you might find some test cases you could
borrow wholesale. Pandas is BSD licensed, so you may even be able to copy
them directly into numpy.

Best,
Stephan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20140418/200d6459/attachment.html>


More information about the NumPy-Discussion mailing list