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

Stephan Hoyer shoyer at gmail.com
Fri Apr 11 19:58:22 EDT 2014


On Fri, Apr 11, 2014 at 3:56 PM, Charles R Harris <charlesr.harris at gmail.com
> wrote:

> Are we in a position to start looking at implementation? If so, it would
> be useful to have a collection of test cases, i.e., typical uses with
> specified results. That should also cover conversion from/(to?)
> datetime.datetime.
>

Indeed, my personal wish-list for np.datetime64 is centered much more on
robust conversion to/from native date objects, including comparison.

Here are some of my particular points of frustration (apologies for the
thread jacking!):
- NaT should have similar behavior to NaN when used for comparisons (i.e.,
comparisons should always be False).
- You can't compare a datetime object to a datetime64 object.
- datetime64 objects with high precision (e.g., ns) can't compare to
datetime objects.

Pandas has a very nice wrapper around datetime64 arrays that solves most of
these issues, but it would be nice to get much of that functionality in
core numpy, since I don't always want to store my values in a 1-dimensional
array + hash-table (the pandas Index):
http://pandas.pydata.org/pandas-docs/stable/timeseries.html

Here's code which reproduces all of the above:

from numpy import datetime64
from datetime import datetime

print np.datetime64('NaT') < np.datetime64('2011-01-01') # this should not
to true
print datetime(2010, 1, 1) < np.datetime64('2011-01-01') # raises exception
print np.datetime64('2011-01-01T00:00', 'ns') > datetime(2010, 1, 1) #
another exception
print np.datetime64('2011-01-01T00:00') > datetime(2010, 1, 1) # finally
something works!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20140411/080f2f7e/attachment.html>


More information about the NumPy-Discussion mailing list