如何将具有可变格式的字符串数据转换为datetime?

时间:2017-09-25 12:28:17

标签: python datetime dataframe

我有一个Pandas数据框,其字符串格式的时间列如下:

 id       playTime

 12       1d 13h 23m
 13      42d 22h 47m
 14       7d 16h 31m
 15        1h 26m 6s 
 16           9d 58m

tried pd.to_datetime(df['playTime'], format='%j%H%M%S')但它显示time data does not match format '%j%H%M%S' (match). In the end, I'd like to convert the df ['playTime'] values to seconds.

2 个答案:

答案 0 :(得分:1)

对于to_timedelta Timedelta来说,我认为total_seconds需要Usage of /posts/{ids}/revisions

df['playTime'] = pd.to_timedelta(df['playTime']).dt.total_seconds().astype(int)
print (df)
   id  playTime
0  12    134580
1  13   3710820
2  14    664260
3  15      5166
4  16    781080

答案 1 :(得分:0)

你可以试试这个:

pd.to_datetime(df['playTime'], format='%j %H %M %S')