2

This is my dataframe. I want to calculate in a new column the difference between those two datetime value in minutes.

 Start date           End date  
    11/30/2011 23:58    12/01/2011 0:26
    11/30/2011 23:56    12/01/2011 0:01
    11/30/2011 23:18    11/30/2011 23:36

23:36

I tried and got this: df['trip_duration] = pd.to_datetime(df['Start date']) - pd.to_datetime(df['End date'])

0 -1 days +23:32:00
1 -1 days +23:55:00
2 -1 days +23:42:00

How do I get the difference in minutes like I mentioned above?

Rijo Simon
  • 777
  • 3
  • 15
  • 35
mike_duaud
  • 23
  • 3
  • yo, this must help you https://stackoverflow.com/questions/1345827/how-do-i-find-the-time-difference-between-two-datetime-objects-in-python – polukarp Mar 22 '21 at 18:55

2 Answers2

1

First, make sure your date columns are already in datetime format:

df['Start date'] = pd.to_datetime(df['Start date'])
df['End date'] = pd.to_datetime(df['End date'])

Then, you can get the time differences of the 2 columns by direct subtraction and then divide by pd.Timedelta('1 minute'), like this:

df['trip_duration'] = (df['End date'] - df['Start date']) / pd.Timedelta('1 minute')

Resulting df:

           Start date            End date  trip_duration
0 2011-11-30 23:58:00 2011-12-01 00:26:00           28.0
1 2011-11-30 23:56:00 2011-12-01 00:01:00            5.0
2 2011-11-30 23:18:00 2011-11-30 23:36:00           18.0
SeaBean
  • 22,547
  • 3
  • 13
  • 25
  • @mike_duaud Note that you don't need to call `pd.to_timedelta` since the difference between 2 datetime values is already in timedelta format. – SeaBean Mar 23 '21 at 12:02
0

the api is not super intuitive with some of the datetime stuff. You can use this:

df['trip_duration'] = pd.to_timedelta(df['End date'] - df['Start date']).dt.seconds/60
anon01
  • 10,618
  • 8
  • 35
  • 58