一列pandas数据帧的小时差

时间:2017-06-22 06:31:34

标签: python pandas

id  time_taken
1   2017-06-21 07:36:53
2   2017-06-21 07:32:28
3   2017-06-22 08:55:09
4   2017-06-22 08:04:31
5   2017-06-21 03:38:46

current_time = 2017-06-22 10:08:16

我想用current_time创建df2,其中time_taken列的时差大于24小时

1   2017-06-21 07:36:53
2   2017-06-21 07:32:28
5   2017-06-21 03:38:46

1 个答案:

答案 0 :(得分:1)

您可以将Timedelta转换为total_seconds并与Timedelta进行比较或比较,按boolean indexing进行过滤:

current_time = '2017-06-22 10:08:16'

df['time_taken'] = pd.to_datetime(df['time_taken'])
df = df[(pd.to_datetime(current_time) - df['time_taken']).dt.total_seconds() > 60 * 60 * 24]
print (df)
   id          time_taken
0   1 2017-06-21 07:36:53
1   2 2017-06-21 07:32:28
4   5 2017-06-21 03:38:46

或者:

df = df[(pd.to_datetime(current_time) - df['time_taken']) > pd.Timedelta(24, unit='h')]
print (df)
   id          time_taken
0   1 2017-06-21 07:36:53
1   2 2017-06-21 07:32:28
4   5 2017-06-21 03:38:46