简体   繁体   中英

Converting string in '[hh]:mm:ss' format to datetime

I am managing a data frame in python that has one column with durations of incidents. The type of the original data frame is a string and I want to convert to DateTime.

The problem is that the format of the string is ' [hh]:mm: ss ', that means that if the duration its more than 24 hours do not count in days but in hours. Example:

>>> df
   duration Location
0  00:01:30        a
1  12:12:04        b
2  26:45:05        c
3  45:01:12        d

I'm trying with different formats but does not work:

df['Duration'] =pd.to_datetime(df['Duracion'], format='%[H]:%M:%S')

Any help?

试试pandas.to_timedelta

df['Duration'] = pd.to_timedelta(df['Duration'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM