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
答案 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