BrianC
BrianC

Reputation: 29

ValueError: time data '12/31/2017' does not match format '%d-%m-%Y'

I am processing date data and can't see an answer to the error as the format does match my date data:

"12/31/2017"

"12/31/2017"

"1/1/2018"

"1/1/2018"

"1/1/2018"

"1/1/2018"

But when I try to convert I get this error

ValueError: time data '12/31/2017' does not match format '%d-%m-%Y'

I have tried switching the %m-%d around just to test but still get the same error.

This is my code for trying to convert the date data:

from datetime import datetime
data.Date = data.Date.apply(lambda x:datetime.strptime(x, '%m-%d-%Y'))

If somebody gives me a clue how to resolve this, any help would be greatly appreciated?

Upvotes: 1

Views: 325

Answers (1)

Luca Bezerra
Luca Bezerra

Reputation: 1188

12/31/2017 is in the format M/D/Y, since you can't have months with value 31 :)

Also, you are forgetting to account for the forward slashes. Try this:

from datetime import datetime
data.Date = data.Date.apply(lambda x:datetime.strptime(x, '%m/%d/%Y'))

Upvotes: 1

Related Questions