Question about SArray.str_to_datetime

User 1014 | 12/1/2014, 7:05:02 PM

The SArray.strtodatetime function fails to convert strings where the day, hour, etc., are not zero-padded. For example, it fails to convert '10/30/2014 9:01' with the template: '%m/%d/%Y %H:%M' because the hour '9' is not '09'. Do I really need to add a leading '0' character where it's missing before using strtodatetime? Is there a better way? Thanks in advance for any suggestion.

Comments

User 954 | 12/1/2014, 10:31:19 PM

Hi, Sorry for the inconvenience.

strtodatetime() function internally uses c++ boost.datetime library (http://www.boost.org/doc/libs/1480/doc/html/datetime/datetimeio.html). Currently boost.date_time lacks support of single digit days,hours,etc.

As a workaround, you can try the following: <pre class="CodeBlock"><code> import graphlab as gl import datetime sf = gl.SFrame({'date':['10/30/2014 9:01']}) sf['datetime'] = sf['date'].apply(lambda x : datetime.datetime.strptime(x, '%m/%d/%Y %H:%M')) </code></pre>

The Python datetime library supports the single-character representation of days, hours, etc. So in this workaround we create a new column in the SFrame to the formatted datetime, and you can see that this new column has datetime type.

<pre class="CodeBlock"><code> In [18]: sf Out[18]: Columns: date str datetime datetime

Rows: 1

Data: +-----------------+---------------------------+ | date | datetime | +-----------------+---------------------------+ | 10/30/2014 9:01 | 2014-10-30 09:01:00+00:00 | +-----------------+---------------------------+ [1 rows x 2 columns]</code></pre>

I hope this helps, please let us know if you have further questions!


User 954 | 1/23/2015, 6:15:04 PM

Hi, I wanted to update you regarding this issue.

This issue is fixed and zero-padding support is available in the next release of the GraphLab Create.