我有日期数据,我试图计算连续行之间的秒数。
我的数据
date
0 2014-05-01 18:47:05
1 2014-05-01 18:47:25
2 2014-05-02 18:47:45
3 2014-05-02 18:48:05
4 2014-05-02 18:48:55
以下是我的尝试:
df['time_diff'] = (df['date']-df['date'].shift()).fillna(0)
df['second'] = df['time_diff'].apply(lambda x: x / np.timedelta64(1,'s')).astype('int64') % (24*60)
但我的第二栏只显示了当时秒段之间的差异。不是从整个时间。
date time_diff second
0 2014-05-01 18:47:05 0 days 00:00:00 0
1 2014-05-01 18:47:25 0 days 00:00:20 20
2 2014-05-02 18:47:45 1 days 00:00:20 20
3 2014-05-02 18:48:05 0 days 00:00:20 20
4 2014-05-02 18:48:55 0 days 00:00:50 50
答案 0 :(得分:4)
使用diff
和dt.seconds
df.date.diff().dt.seconds
df.assign(seconds=df.date.diff().dt.seconds)
date seconds
0 2014-05-01 18:47:05 NaN
1 2014-05-01 18:47:25 20.0
2 2014-05-02 18:47:45 20.0
3 2014-05-02 18:48:05 20.0
4 2014-05-02 18:48:55 50.0