-3

I have raw data like this want to find the difference between this two time in mint .....problem is data which is in data frame... source:

   start time   end time
0   08:30:00    17:30:00
1   11:00:00    17:30:00
2   08:00:00    21:30:00
3   19:30:00    22:00:00
4   19:00:00    00:00:00
5   08:30:00    15:30:00

Need a output like this:

duration
 540mint
 798mint
 162mint
 1140mint
 420mint
viki
  • 1
  • 3

3 Answers3

0

Your expected output seems to be incorrect. That aside, we can use base R's difftime:

transform(
    df,
    duration = difftime(
        strptime(end.time, format = "%H:%M:%S"),
        strptime(start.time, format = "%H:%M:%S"),
        units = "mins"))
#  start.time end.time   duration
#0   08:30:00 17:30:00   540 mins
#1   11:00:00 17:30:00   390 mins
#2   08:00:00 21:30:00   810 mins
#3   19:30:00 22:00:00   150 mins
#4   19:00:00 00:00:00 -1140 mins
#5   08:30:00 15:30:00   420 mins

or as a difftime vector

with(df, difftime(
    strptime(end.time, format = "%H:%M:%S"),
    strptime(start.time, format = "%H:%M:%S"),
    units = "mins"))
#Time differences in mins
#[1]   540   390   810   150 -1140   420

Sample data

df <- read.table(text =
    "   'start time'   'end time'
0   08:30:00    17:30:00
1   11:00:00    17:30:00
2   08:00:00    21:30:00
3   19:30:00    22:00:00
4   19:00:00    00:00:00
5   08:30:00    15:30:00", header = T, row.names = 1)
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
0
import pandas as pd

df = pd.DataFrame({'start time':['08:30:00','11:00:00','08:00:00','19:30:00','19:00:00','08:30:00'],'end time':['17:30:00','17:30:00','21:30:00','22:00:00','00:00:00','15:30:00']},columns=['start time','end time'])

df
Out[355]:
  start time  end time
0   08:30:00  17:30:00
1   11:00:00  17:30:00
2   08:00:00  21:30:00
3   19:30:00  22:00:00
4   19:00:00  00:00:00
5   08:30:00  15:30:00

(pd.to_datetime(df['end time']) - pd.to_datetime(df['start time'])).dt.seconds/60
Out[356]:
0    540.0
1    390.0
2    810.0
3    150.0
4    300.0
5    420.0
dtype: float64
cosmic_inquiry
  • 2,557
  • 11
  • 23
-1

Yes, definitely datetime is what you need here. Specifically, the strptime function, which parses a string into a time object.

from datetime import datetime
s1 = '10:33:26'
s2 = '11:15:49' # for example
FMT = '%H:%M:%S'
tdelta = datetime.strptime(s2, FMT) - datetime.strptime(s1, FMT)

That gets you a timedelta object that contains the difference between the two times. You can do whatever you want with that, e.g. converting it to seconds or adding it to another datetime.

This will return a negative result if the end time is earlier than the start time, for example s1 = 12:00:00 and s2 = 05:00:00. If you want the code to assume the interval crosses midnight in this case (i.e. it should assume the end time is never earlier than the start time), you can add the following lines to the above code:

if tdelta.days < 0:
    tdelta = timedelta(days=0,
                seconds=tdelta.seconds, microseconds=tdelta.microseconds)

(of course you need to include from datetime import timedelta somewhere). Thanks to J.F. Sebastian for pointing out this use case.

Morteza M
  • 167
  • 1
  • 3
  • 12
  • 1
    see [more](https://stackoverflow.com/questions/3096953/how-to-calculate-the-time-interval-between-two-time-strings/3096984#3096984) – Morteza M Jun 08 '18 at 06:00