[Numpy-discussion] Implicit conversion of python datetime to numpy datetime64?

Mark Wiebe mwwiebe at gmail.com
Wed Feb 15 00:12:20 EST 2012


On Tue, Feb 14, 2012 at 8:17 PM, Benjamin Root <ben.root at ou.edu> wrote:

> 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.
>

I would consider this a bug, it's not behaving sensibly at present. Here's
what it does for me:

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?
>

arange definitely works:

In[28] np.arange('2011-03-02', '2011-04-01', dtype='M8')
Out[28]:
array(['2011-03-02', '2011-03-03', '2011-03-04', '2011-03-05',
       '2011-03-06', '2011-03-07', '2011-03-08', '2011-03-09',
       '2011-03-10', '2011-03-11', '2011-03-12', '2011-03-13',
       '2011-03-14', '2011-03-15', '2011-03-16', '2011-03-17',
       '2011-03-18', '2011-03-19', '2011-03-20', '2011-03-21',
       '2011-03-22', '2011-03-23', '2011-03-24', '2011-03-25',
       '2011-03-26', '2011-03-27', '2011-03-28', '2011-03-29',
       '2011-03-30', '2011-03-31'], dtype='datetime64[D]')

I didn't get to implementing linspace. I did look at it, but the current
code didn't make it a trivial thing to put in.

-Mark

> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20120214/4f57af2e/attachment.html>


More information about the NumPy-Discussion mailing list