I have duration string noted as '48m 37s' and sometimes as '1h 38m 29s' and a collection of string that holds this data in pandas dataframe's column
I'm trying to convert datatype of duration column to datetime as follows
pd.to_datetime(usg['duration'], format='%Hh %Mm %Ss')
but failing with the following error
ValueError: time data '1h 38m 29s' does not match format '%Mm %Ss' (match)
I understand that sometimes the hour part is missing in the list entries of duration column and wondering if there is a way to specify multiple formats if in case one fails to match other succeeds.
Doing so shall yield the output as below preserving the order of entries in the column
00:39:40
01:38:29
07:39:40
Better use: pd.to_timedelta(usg['duration'])
usg = pd.DataFrame({'duration': ['48m 37s', '1h 38m 29s']})
pd.to_timedelta(usg['duration'])
gives the output:
0 00:48:37
1 01:38:29
Name: duration, dtype: timedelta64[ns]