Reputation:
I am trying to convert a pandas data frame column from string to a datetime type.I am sure I am doing it correctly but am getting an error saying that the format does not match and I can not work out why. My strings I want to convert look like this
Date
2019-02-03 04:09:34
2019-02-02 14:21:03
2019-02-02 16:54:13
2019-02-02 17:39:19
2019-02-02 09:13:38
2019-01-05 09:03:24
2019-02-02 16:50:34
2019-02-02 16:05:50
2019-02-02 07:28:10
I am trying this on the file containing this data
file['Date1'] = pd.to_datetime(file['Date'], format='%Y-%m-%d :%H:%M:%S')`
But repetitively get the error
ValueError: time data ' Date' does not match format '%Y-%m-%d :%H:%M:%S' (match)`
I have been able to make this work but only for one row not the entire column
file['Date1'] = datetime.strptime(file['Date'][1], '%Y-%m-%d %H:%M:%S')
Please let me know what I am doing wrong, Thank you
Upvotes: 0
Views: 63
Reputation: 19895
The problem is your additional :
before %:H
in your format string. pandas
is looking for a colon and can't find it in the data you provide.
Additionally, I tested pd.to_datetime
without a format string, and it appears to be able to infer the format, so you could do that too.
Upvotes: 1