将字符串转回日期时间timedelta

时间:2017-06-18 03:59:54

标签: python pandas datetime timedelta

我的pandas数据框中的一列表示我使用datetime计算的时间增量,然后导出到csv并读回到pandas数据框中。现在列的dtype是对象,而我希望它是timedelta,所以我可以在数据帧上执行groupby函数。下面是字符串的样子。谢谢!

  0 days 00:00:57.416000
  0 days 00:00:12.036000
  0 days 16:46:23.127000  
 49 days 00:09:30.813000  
 50 days 00:39:31.306000  
 55 days 12:39:32.269000
-1 days +22:03:05.256000

更新,我最好尝试编写一个for循环迭代我的pandas数据帧中的特定列:

def delta(i):
    days, timestamp = i.split(" days ")
    timestamp = timestamp[:len(timestamp)-7]
    t = datetime.datetime.strptime(timestamp,"%H:%M:%S") + 
    datetime.timedelta(days=int(days))
    delta = datetime.timedelta(days=t.day, hours=t.hour, 
    minutes=t.minute, seconds=t.second)
    delta.total_seconds()

data['diff'].map(delta)

3 个答案:

答案 0 :(得分:3)

使用pd.to_timedelta

pd.to_timedelta(df.iloc[:, 0])

0     0 days 00:00:57.416000
1     0 days 00:00:12.036000
2     0 days 16:46:23.127000
3    49 days 00:09:30.813000
4    50 days 00:39:31.306000
5    55 days 12:39:32.269000
6   -1 days +22:03:05.256000
Name: 0, dtype: timedelta64[ns]

答案 1 :(得分:1)

你可以这样做,循环遍历CSV中的每个值代替stringdate:

stringdate = "2 days 00:00:57.416000"
days_v_hms = string1.split('days')
hms = days_v_hms[1].split(':')
dt = datetime.timedelta(days=int(days_v_hms[0]), hours=int(hms[0]), minutes=int(hms[1]), seconds=float(hms[2]))

干杯!

答案 2 :(得分:1)

import datetime

#Parse your string
days, timestamp = "55 days 12:39:32.269000".split(" days ")
timestamp = timestamp[:len(timestamp)-7]

#Generate datetime object
t = datetime.datetime.strptime(timestamp,"%H:%M:%S") + datetime.timedelta(days=int(days))

#Generate a timedelta
delta = datetime.timedelta(days=t.day, hours=t.hour, minutes=t.minute, seconds=t.second)

#Represent in Seconds
delta.total_seconds()