Back
I have a Pandas data frame, one of the columns of which contains date strings in the format 'YYYY-MM-DD' e.g. '2013-10-28'.
At the moment the dtype of the column is 'object'.
How do I convert the column values to Pandas date format?
Use astype:
In [31]: dfOut[31]: a time0 1 2013-01-011 2 2013-01-022 3 2013-01-03In [32]: df['time'] = df['time'].astype('datetime64[ns]')In [33]: dfOut[33]: a time0 1 2013-01-01 00:00:001 2 2013-01-02 00:00:002 3 2013-01-03 00:00:00
In [31]: df
Out[31]:
a time
0 1 2013-01-01
1 2 2013-01-02
2 3 2013-01-03
In [32]: df['time'] = df['time'].astype('datetime64[ns]')
In [33]: df
Out[33]:
0 1 2013-01-01 00:00:00
1 2 2013-01-02 00:00:00
2 3 2013-01-03 00:00:00
If you want to learn more about Pandas then visit this Python Course designed by the industrial experts.
31k questions
32.8k answers
501 comments
693 users