Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python Datetime parsing using AM not matching due to " "

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#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

>Solution :

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;

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading