Pandas.to_datetime doesn't recognize the format of the string to be converted to datetime

Question:

I am trying to convert some data from a .txt file to a dataframe to use it for some analysis

the form of the data in the .txt is a follows

DATE_TIME   VELOC   MEASURE 
    [m/s]   [l/h]   

A 09.01.2023 12:45:20 ??? ???
A 09.01.2023 12:46:20 0,048 52,67
A 09.01.2023 12:47:20 0,049 53,77
A 09.01.2023 12:48:20 0,050 54,86

I load the data to a dataframe no problem i covnert the str values of the measurement to float etc everything is good as shows in the
image

the problem I get is when trying to convert the column of the date time that is string to datetime pandas format using this line of code:

volume_flow[‘DATE_TIME’] = pd.to_datetime(volume_flow[‘DATE_TIME’], format = ‘%d.%m.%Y %H:%M:S’)

and i get the following error

ValueError: time data ‘09.01.2023 12:46:20’ does not match format ‘%d.%m.%Y %H:%M:S’ (match)

but i don’t see how the format is off

I am really lost as to why this is caused as i used the same code with different formats of datetime before with no problem

further more i tried using format = ‘%dd.%mm.%yyyy %H:%M:S’ as well with the same results and when i let the pandas.to_datetime convert it automatically it confuses the day and the month of the data. the data is between 09.01-12.01 so you can’t really tell if one is the month or day just by the values.

Asked By: joyless_69

||

Answers:

I think you should go from this

(..., format='%d.%m.%Y %H:%M:S')

to this

(..., format='%d.%m.%Y %H:%M:%S')

You forgot the percentage character!

Answered By: rafidini

check the documentations for correct time format. You will note that the directive %S represents the seconds.

Second as a decimal number [00,61].

enter image description here

Answered By: pylos
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.