Search code examples
pythonpandasdate-format

How to convert from DataFrame row to datetime format?


I have the following df

0    05/08/2013 16:04:02:155
1    05/08/2013 16:04:02:170
2    05/08/2013 16:04:02:217
3    10/07/2013 16:40:51:787
4    07/24/2014 09:50:30:228
Name: ctimestamp, dtype: object

I need convert to date time

df['ctimestamp'] = pd.to_datetime(df['ctimestamp'])

But I have this error

raise ParserError("Unknown string format: %s", timestr)
dateutil.parser._parser.ParserError: Unknown string format: 05/08/2013 16: 04: 02: 155
make: *** [test] Error 1

Solution

  • You need to specify the date format for your string because it is not in the most popular format:

    pd.to_datetime(df['ctimestamp'], format="%m/%d/%Y %H:%M:%S:%f")
    

    This will work.