使用python在df中找出日期之间的差异

时间:2016-06-24 16:20:54

标签: python datetime pandas

我有数据

                   date                      id       request  
0   2016-06-22 03:00:48         upi88@yandex.ru  GET HTTP/1.1   
1   2016-06-22 03:02:52         upi88@yandex.ru  GET HTTP/1.1   
2   2016-06-22 03:03:48         upi88@yandex.ru  GET HTTP/1.1   
3   2016-06-22 03:05:15         upi88@yandex.ru  GET HTTP/1.1   
4   2016-06-22 03:05:55         upi88@yandex.ru  GET HTTP/1.1 

我希望计算date之间的差异 我试试

dates = df1['date']
for i, date in enumerate(dates):
    dates[i].to_datetime()
    d = (dates[i] - dates[i-1]).total_seconds()

但它返回

KeyError: -1L

2 个答案:

答案 0 :(得分:0)

是你想要的吗?

In [130]: df
Out[130]:
                 date               id         request
0 2016-06-22 03:00:48  upi88@yandex.ru  GET HTTP/1.1
1 2016-06-22 03:02:52  upi88@yandex.ru  GET HTTP/1.1
2 2016-06-22 03:03:48  upi88@yandex.ru  GET HTTP/1.1
3 2016-06-22 03:05:15  upi88@yandex.ru  GET HTTP/1.1
4 2016-06-22 03:05:55  upi88@yandex.ru    GET HTTP/1.1

In [131]: df.date.diff().dt.total_seconds()
Out[131]:
0      NaN
1    124.0
2     56.0
3     87.0
4     40.0
Name: date, dtype: float64

答案 1 :(得分:0)

你的代码起始索引是1而i-1是-1

dates = df1['date']
for i in range(1,len(dates)):
    dates[i].to_datetime()
    d = (dates[i] - dates[i-1]).total_seconds()