Just a thought I had. Right now, I can pass a list of python ints or floats into np.array() and get a numpy array with a sensible dtype. Is there any reason why we can't do the same for python's datetime? Right now, it is very easy for me to make a list comprehension of datetime objects using strptime(), but it is very awkward to make a numpy array out of it.
In [20]: np.array([datetime.datetime.strptime(date, "%m/%d/%y") for date in ["02/03/12",
...: "07/22/98", "12/12/12"]], dtype="M8")
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
C:\Python27\Scripts\<ipython-input-20-d3b7b5392190> in <module>()
1 np.array([datetime.datetime.strptime(date, "%m/%d/%y") for date in ["02/03/12",
----> 2 "07/22/98", "12/12/12"]], dtype="M8")
TypeError: Cannot cast datetime.datetime object from metadata [us] to [D] according to the rule 'same_kind'
In [21]: np.array([datetime.datetime.strptime(date, "%m/%d/%y") for date in ["02/03/12",
...: "07/22/98", "12/12/12"]], dtype="M8[us]")
Out[21]:
array(['2012-02-02T16:00:00.000000-0800',
'1998-07-21T17:00:00.000000-0700', '2012-12-11T16:00:00.000000-0800'], dtype='datetime64[us]')
In [22]: np.array([datetime.datetime.strptime(date, "%m/%d/%y") for date in ["02/03/12",
...: "07/22/98", "12/12/12"]], dtype="M8[us]").astype("M8[D]")
Out[22]: array(['2012-02-03', '1998-07-22', '2012-12-12'], dtype='datetime64[D]')
The only barrier I can think of are those who have already built code around a object dtype array of datetime objects.
Thoughts?
Ben Root
P.S. - what ever happened to arange() and linspace() for datetime64?
_______________________________________________
NumPy-Discussion mailing list
NumPy-Di...@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion