Python Datetime parsing using AM not matching due to " "

Question:

I am trying to convert a string that includes a date to a datetime object.

How it looks: ’01/01/2017 01:00:00 AM’

What I am currently using:

#df['date'] =  pd.to_datetime(df['date'], format="%m/%d/%Y %I:%M:%S %p")

I get the following error message.

ValueError: time data ' ' does not match format '%m/%d/%Y %I:%M:%S %p' (match)

Thank you in advance for your help

Asked By: cptscottz

||

Answers:

I didn’t want to spend the time to construct a Pandas example. But I figured that Pandas would be using Python’s strftime function to parse dates. So I just created an example with that function

This line of code:

datetime.strptime(' ', '%m/%d/%Y %I:%M:%S %p')

gives me the error

ValueError: time data ' ' does not match format '%m/%d/%Y %I:%M:%S %p'. 

Same exact error. So somewhere you think you’ve got a valid date string, but you actually have a string that’s a single space character;

Answered By: CryptoFool
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.