<div class="gmail_quote">On Tue, Feb 14, 2012 at 8:17 PM, Benjamin Root <span dir="ltr"><<a href="mailto:ben.root@ou.edu">ben.root@ou.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
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.<br>
</blockquote><div><br></div><div>I would consider this a bug, it's not behaving sensibly at present. Here's what it does for me:</div><div><br></div><div><div><p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px">
<span style="color:#000080">In [</span><span style="font-weight:600;color:#000080">20</span><span style="color:#000080">]:</span> np.array([datetime.datetime.strptime(date, "%m/%d/%y") for date in ["02/03/12",</p>

<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px"><span style="color:#000080">    ...:</span>     "07/22/98", "12/12/12"]], dtype="M8")</p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px"><span style="color:#8b0000">---------------------------------------------------------------------------</span></p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px"><span style="color:#8b0000">TypeError</span>                                 Traceback (most recent call last)</p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px"><span style="color:#006400">C:\Python27\Scripts\<ipython-input-20-d3b7b5392190></span> in <span style="color:#4682b4"><module></span><span style="color:#00008b">()</span></p>

<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px"><span style="color:#006400">      1</span> np.array([datetime.datetime.strptime(date, "%m/%d/%y") for date in ["02/03/12",</p>

<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px"><span style="color:#006400">----> 2</span><span style="color:#a52a2a">     "07/22/98", "12/12/12"]], dtype="M8")</span></p>

<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;color:rgb(165,42,42)"></p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px"><span style="color:#8b0000">TypeError</span>: Cannot cast datetime.datetime object from metadata [us] to [D] according to the rule 'same_kind'</p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px"><br></p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px"></p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px"><span style="color:#000080">In [</span><span style="font-weight:600;color:#000080">21</span><span style="color:#000080">]:</span> np.array([datetime.datetime.strptime(date, "%m/%d/%y") for date in ["02/03/12",</p>

<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px"><span style="color:#000080">    ...:</span>     "07/22/98", "12/12/12"]], dtype="M8[us]")</p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px"><span style="color:#8b0000">Out[</span><span style="font-weight:600;color:#8b0000">21</span><span style="color:#8b0000">]:</span> </p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px">array(['2012-02-02T16:00:00.000000-0800',</p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px">       '1998-07-21T17:00:00.000000-0700', '2012-12-11T16:00:00.000000-0800'], dtype='datetime64[us]')</p><p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px">
<br></p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px"></p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px"><span style="color:#000080">In [</span><span style="font-weight:600;color:#000080">22</span><span style="color:#000080">]:</span> np.array([datetime.datetime.strptime(date, "%m/%d/%y") for date in ["02/03/12",</p>

<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px"><span style="color:#000080">    ...:</span>     "07/22/98", "12/12/12"]], dtype="M8[us]").astype("M8[D]")</p>

<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px"><span style="color:#8b0000">Out[</span><span style="font-weight:600;color:#8b0000">22</span><span style="color:#8b0000">]:</span> array(['2012-02-03', '1998-07-22', '2012-12-12'], dtype='datetime64[D]')</p>
</div></div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">The only barrier I can think of are those who have already built code around a object dtype array of datetime objects.<br>
<br>Thoughts?<br>Ben Root<br><br>P.S. - what ever happened to arange() and linspace() for datetime64?<br></blockquote><div><br></div><div>arange definitely works:</div><div><br></div><div>In[28] np.arange('2011-03-02', '2011-04-01', dtype='M8')</div>
<div>Out[28]: </div><div>array(['2011-03-02', '2011-03-03', '2011-03-04', '2011-03-05',</div><div>       '2011-03-06', '2011-03-07', '2011-03-08', '2011-03-09',</div>
<div>       '2011-03-10', '2011-03-11', '2011-03-12', '2011-03-13',</div><div>       '2011-03-14', '2011-03-15', '2011-03-16', '2011-03-17',</div><div>       '2011-03-18', '2011-03-19', '2011-03-20', '2011-03-21',</div>
<div>       '2011-03-22', '2011-03-23', '2011-03-24', '2011-03-25',</div><div>       '2011-03-26', '2011-03-27', '2011-03-28', '2011-03-29',</div><div>       '2011-03-30', '2011-03-31'], dtype='datetime64[D]')</div>
<div><br></div><div>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.</div><div><br></div><div>-Mark</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
<br></blockquote></div><br>