从熊猫数据框中获取timedelta

时间:2014-03-25 10:02:31

标签: python string pandas apply timedelta

我有一个包含这样的时间戳的pandas数据框

     time_0            time_1            time_2            time_3  
21/03/2014 16:17  21/03/2014 15:40  21/03/2014 14:55  21/03/2014 12:50   
21/03/2014 16:29  21/03/2014 16:26  21/03/2014 16:23  21/03/2014 16:21  
04/07/2012 13:43  04/07/2012 13:37  04/07/2012 13:34  04/07/2012 13:29  
19/03/2014 01:41  18/03/2014 01:19  17/03/2014 00:50  05/03/2014 22:30   
21/01/2010 17:22  21/01/2010 17:21  21/01/2010 17:21  21/01/2010 17:21   

我想从每一列到下一列(如果您愿意,可以使用时间增量)。我想要的输出是:time_0-time_1,time_1-time_2,time_2-time_3等。

我在执行简单的列[i] -column [i + 1]时遇到的问题是这种错误:

Traceback (most recent call last):
File "D:\TwitterFakeDetect\src\root\userinforetrieval\reformat.py", line 22, in <module>
deltadatas[column_names[0]]=deltadatas[column_names[0]]-deltadatas[column_names[1]]
File "C:\Users\SC5\Anaconda\lib\site-packages\pandas\core\ops.py", line 496, in wrapper
arr = na_op(lvalues, rvalues)
File "C:\Users\SC5\Anaconda\lib\site-packages\pandas\core\ops.py", line 449, in na_op
result[mask] = op(x[mask], y[mask])
TypeError: unsupported operand type(s) for -: 'str' and 'str'

所以我认为这不起作用,因为我有字符串而不是日期。我创建了一个函数来转换我的数据框:

def convert_string_to_date(x):
        datetime.strptime(i, '%d/%m/%Y %H:%M')

然后在pandas中使用内置的apply函数。但后来,我发现我的日期实际上都不是字符串,所以这并没有真正解决问题。

Traceback (most recent call last):
  File "D:\TwitterFakeDetect\src\root\userinforetrieval\reformat.py", line 14, in <module>
    deltadatas.apply(convert_string_to_date, axis=1)
  File "C:\Users\SC5\Anaconda\lib\site-packages\pandas\core\frame.py", line 3424, in apply
    return self._apply_standard(f, axis, reduce=reduce)
  File "C:\Users\SC5\Anaconda\lib\site-packages\pandas\core\frame.py", line 3494, in _apply_standard
    results[i] = func(v)
  File "D:\TwitterFakeDetect\src\root\userinforetrieval\reformat.py", line 12, in convert_string_to_date
    datetime.strptime(i, '%d/%m/%Y %H:%M')
TypeError: ('must be string, not int', u'occurred at index 0')

总结一下,有没有办法检查我的数据框内容的类型,确定为什么我不能像我想象的那样轻松地获得我的列的减法?

感谢您的帮助!我真的被这个人困住了!

1 个答案:

答案 0 :(得分:3)

在读入时解析日期时间(列表列表在单独的列中解析日期和时间(请注意,为简单起见,这会从上面跳过标题)

In [23]: df = read_csv(StringIO(data),sep='\s+',header=None,parse_dates=[[0,1],[2,3],[4,5],[6,7]])

In [24]: df.columns = ['date1','date2','date3','date4']

In [25]: df
Out[25]: 
                date1               date2               date3               date4
0 2014-03-21 16:17:00 2014-03-21 15:40:00 2014-03-21 14:55:00 2014-03-21 12:50:00
1 2014-03-21 16:29:00 2014-03-21 16:26:00 2014-03-21 16:23:00 2014-03-21 16:21:00
2 2012-04-07 13:43:00 2012-04-07 13:37:00 2012-04-07 13:34:00 2012-04-07 13:29:00
3 2014-03-19 01:41:00 2014-03-18 01:19:00 2014-03-17 00:50:00 2014-05-03 22:30:00
4 2010-01-21 17:22:00 2010-01-21 17:21:00 2010-01-21 17:21:00 2010-01-21 17:21:00

[5 rows x 4 columns]

Timedeltas很简单

In [26]: DataFrame(dict(td1 = df['date1']-df['date2'], td2 = df['date2']-df['date3'], td3=df['date3']-df['date4']))
Out[26]: 
               td1              td2                td3
0 0 days, 00:37:00 0 days, 00:45:00   0 days, 02:05:00
1 0 days, 00:03:00 0 days, 00:03:00   0 days, 00:02:00
2 0 days, 00:06:00 0 days, 00:03:00   0 days, 00:05:00
3 1 days, 00:22:00 1 days, 00:29:00 -47 days, 21:40:00
4 0 days, 00:01:00 0 days, 00:00:00   0 days, 00:00:00

[5 rows x 3 columns]