using astype('datetime64[ns]')

3,555 views
Skip to first unread message

Arun Kumar

unread,
Mar 16, 2018, 2:40:28 AM3/16/18
to PyData
Hi All,

I'm trying to convert the object/string column to date format using the astype('datetime64[ns]') method. However I'm getting the below error. Any ideas of why this is happening. I'm aware that we can instead use the pd.to_datetime() method but I'd like to try with astype() method as well. Any ideas would be of great help. Also a good tutorial link on using astype('datetime64[ns]') would be highly appreciated.

ValueError: Error parsing datetime string "5/22/2017" at position 1

Thanks,
Arun

Pietro Battiston

unread,
Mar 16, 2018, 4:22:49 AM3/16/18
to pyd...@googlegroups.com
Hi Arun,

Il giorno gio, 15/03/2018 alle 23.40 -0700, Arun Kumar ha scritto:
> Hi All,
>
> I'm trying to convert the object/string column to date format using
> the astype('datetime64[ns]') method. However I'm getting the below
> error.

pd.Series(['2017-05-22']).astype('datetime64[ns]')

works. But in general, ".astype('datetime64[ns]')" is much less
flexible, in terms of date format, than pd.to_datetime() (which also
supports "format=").

Pietro

Stephan Hoyer

unread,
Mar 16, 2018, 1:38:13 PM3/16/18
to pyd...@googlegroups.com
My guess is that astype('datetime64[ns]') goes through NumPy's datetime64 machinery, which only parses ISO-8601 strings.

Possibly astype() in pandas should be adjusted to use to_datetime() when appropriate.

--
You received this message because you are subscribed to the Google Groups "PyData" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pydata+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages