I am trying to convert a date & time string using Pandas ‘to_datetime’, but the string values is non-zero padded:
3/31/22 23:30 3/31/22 23:45 4/1/22 0:00 4/1/22 0:15
I have the following but get a mismatch error
pd.to_datetime(df.TimeStamp, format="%m/%d/%y %H:%m")
Is there a way to add the zero padding or have ‘to_datetime’ accept the above formatting?
Advertisement
Answer
The trouble isn’t in the padding, it’s actually in your formatting call. Note the capitalization of minutes (M) vs months (m), you used (m) for both. (documentation here).
Demonstration of working code is below
pd.to_datetime(df.TimeStamp, format=”%m/%d/%y %H:%m”)
should be
pd.to_datetime(df.TimeStamp, format=”%M/%d/%y %H:%m”)
import pandas as pd times = [ "3/31/22 23:30", "3/31/22 23:45", "4/1/22 0:00", "4/1/22 0:15" ] df = pd.DataFrame(times, columns=['TimeStamp']) pd.to_datetime(df.TimeStamp, format="%m/%d/%y %H:%M") >> 0 2022-03-31 23:30:00 >> 1 2022-03-31 23:45:00 >> 2 2022-04-01 00:00:00 >> 3 2022-04-01 00:15:00 >> Name: TimeStamp, dtype: datetime64[ns]
That said, if anyone lands here looking for the solution to the zero-padding, the hash/dash trick is worth further reading (though it does not work in many circumstances)