Skip to content Skip to sidebar Skip to footer

Time Difference In Hours Of A Column Of Pandas Dataframe

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 i want t

Solution 1:

You can convert Timedelta to total_seconds and compare or compare with Timedelta, filter by 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

Or:

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

Post a Comment for "Time Difference In Hours Of A Column Of Pandas Dataframe"