Sultanust
Sultanust

Reputation: 33

time data does not match

I have an issue where it says:

time data '2021-08-17 05:42:48 ' does not match format '%y/%m/%d %H:%M:%S' (match)

but my code is:

df['Event_Start_Time'] = pd.to_datetime(df['Event_Start_Time'], format='%y/%m/%d %H:%M:%S')

I am not sure why the two formats are not matching

Upvotes: 1

Views: 97

Answers (2)

Zephyr
Zephyr

Reputation: 12496

As already pointed out by Hampus Larsson in the comment, there are two typos in your code:

  • format code for year is %Y, not %y
  • in your dataframe years, months and days are separated with -, not /

In general, you can refer to this document for the available date and time format codes.
That being said, you should use:

df['Event_Start_Time'] = pd.to_datetime(df['Event_Start_Time'], format='%Y-%m-%d %H:%M:%S')

Upvotes: 1

MDR
MDR

Reputation: 2670

Looks like you're following my code example from the other question where I showed an example of reading in a string like 4/12/19 06:00. Setting the format with / in that case would work but you have to look at what you actually have.

Run df.info() and see if the column is actually datetime already. If not look at the format you have and match it. Probably format='%Y-%m-%d %H:%M:%S' in your case.

Upvotes: 0

Related Questions