how to get time difference in minutes in pandas

Neil

I have following dataframe in pandas

  code     start_time      end_time
  0        13:00:09        13:30:09
  1        14:23:33        15:23:23
  2        11:30:00        12:30:00

start_time and end_time are of type objects.

I want to get difference of these two columns in minutes. I am doing following in pandas

df['time_diff'] = pd.Timestamp(df['start_time']) - pd.Timestamp(df['end_time'])   
jezrael

Convert columns to datetimes by to_datetime or to timedeltas by to_timedelta, subtract by sub, convert output timedeltas to total_seconds and divide 60:

start_time = pd.to_datetime(df['start_time'].astype(str)) 
end_time = pd.to_datetime(df['end_time'].astype(str))

#another solution
#start_time = pd.to_timedelta(df['start_time'].astype(str)) 
#end_time = pd.to_timedelta(df['end_time'].astype(str))

df['time_diff'] = start_time.sub(end_time).dt.total_seconds().div(60)
print (df)
   code start_time  end_time  time_diff
0     0   13:00:09  13:30:09 -30.000000
1     1   14:23:33  15:23:23 -59.833333
2     2   11:30:00  12:30:00 -60.000000

If swap end time with start time:

df['time_diff'] = end_time.sub(start_time).dt.total_seconds().div(60)
print (df)
   code start_time  end_time  time_diff
0     0   13:00:09  13:30:09  30.000000
1     1   14:23:33  15:23:23  59.833333
2     2   11:30:00  12:30:00  60.000000

Sample:

import datetime

df = pd.DataFrame({'code': [0, 1, 2], 
                  'start_time': [datetime.time(13, 0, 9), datetime.time(14, 23, 33), 
                                 datetime.time(11, 30)], 
                   'end_time': [datetime.time(13, 30, 9), datetime.time(15, 23, 23),
                                datetime.time(12, 30)]})

print (df)
   code start_time  end_time
0     0   13:00:09  13:30:09
1     1   14:23:33  15:23:23
2     2   11:30:00  12:30:00

IDE from comment, thanks @Anton vBR:

start_time = pd.to_timedelta(df['start_time'].astype(str)).dt.total_seconds()
end_time = pd.to_timedelta(df['end_time'].astype(str)).dt.total_seconds()

df['time_diff'] = end_time.sub(start_time).div(60)
print (df)
   code start_time  end_time  time_diff
0     0   13:00:09  13:30:09  30.000000
1     1   14:23:33  15:23:23  59.833333
2     2   11:30:00  12:30:00  60.000000

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

how to calculate difference between time in pandas

分類Dev

How to add minutes to time in R

分類Dev

How do I get the difference in minutes between two dates in SSIS using Derived Column transform?

分類Dev

How to calculate time difference between start time and end time by group in pandas?

分類Dev

Calculate Time DIfference Pandas by columns

分類Dev

How to get difference of two pandas indices, but only those of the first index?

分類Dev

How to convert a datetime format to minutes - pandas

分類Dev

Java - Count Difference in minutes

分類Dev

How to get the difference between two date and times and show the time running down

分類Dev

How to fix opening time minutes for whole week using DateTime Picker?

分類Dev

How do I grab a process's start time in minutes?

分類Dev

how to bin unix timestamp time into 10 minutes interval?

分類Dev

How to specify my table to insert data as time in minutes and second into a column?

分類Dev

how to add hours ,minutes or seconds in 'TIME' Datatype in mysql

分類Dev

Getting date time stamp difference minutes in Oracle Database 12c Enterprise Edition

分類Dev

Get the time difference from time varaibles stored as character

分類Dev

Time() By 5 Minutes Select

分類Dev

time lag in minutes in R

分類Dev

How to insert missing time (by minutes) in data frame? And how to assign corresponding y values for that missing time as NA?

分類Dev

How to get dummies of only those values that occur more than X time in Pandas

分類Dev

how to find time difference from row names?

分類Dev

get rows by time regardless of date in pandas

分類Dev

Netezza get records last updated in last 5 minutes using a last modified time stamp column in the table?

分類Dev

How to get the boot time of Ubuntu?

分類Dev

How to get Total Time in a location

分類Dev

How to get Windows boot time?

分類Dev

html - How to show input time show M:S.ms (minutes:second.milisecond)?

分類Dev

How to get the difference between numbers using dc?

分類Dev

How to compare four columns of pandas dataframe at a time?

Related 関連記事

  1. 1

    how to calculate difference between time in pandas

  2. 2

    How to add minutes to time in R

  3. 3

    How do I get the difference in minutes between two dates in SSIS using Derived Column transform?

  4. 4

    How to calculate time difference between start time and end time by group in pandas?

  5. 5

    Calculate Time DIfference Pandas by columns

  6. 6

    How to get difference of two pandas indices, but only those of the first index?

  7. 7

    How to convert a datetime format to minutes - pandas

  8. 8

    Java - Count Difference in minutes

  9. 9

    How to get the difference between two date and times and show the time running down

  10. 10

    How to fix opening time minutes for whole week using DateTime Picker?

  11. 11

    How do I grab a process's start time in minutes?

  12. 12

    how to bin unix timestamp time into 10 minutes interval?

  13. 13

    How to specify my table to insert data as time in minutes and second into a column?

  14. 14

    how to add hours ,minutes or seconds in 'TIME' Datatype in mysql

  15. 15

    Getting date time stamp difference minutes in Oracle Database 12c Enterprise Edition

  16. 16

    Get the time difference from time varaibles stored as character

  17. 17

    Time() By 5 Minutes Select

  18. 18

    time lag in minutes in R

  19. 19

    How to insert missing time (by minutes) in data frame? And how to assign corresponding y values for that missing time as NA?

  20. 20

    How to get dummies of only those values that occur more than X time in Pandas

  21. 21

    how to find time difference from row names?

  22. 22

    get rows by time regardless of date in pandas

  23. 23

    Netezza get records last updated in last 5 minutes using a last modified time stamp column in the table?

  24. 24

    How to get the boot time of Ubuntu?

  25. 25

    How to get Total Time in a location

  26. 26

    How to get Windows boot time?

  27. 27

    html - How to show input time show M:S.ms (minutes:second.milisecond)?

  28. 28

    How to get the difference between numbers using dc?

  29. 29

    How to compare four columns of pandas dataframe at a time?

ホットタグ

アーカイブ