5

I have the following two time column,"Time1" and "Time2".I have to calculate the "Difference" column,which is (Time2-Time1) in Pandas:

Time1         Time2                 Difference
8:59:45       9:27:30               -1 days +23:27:45
9:52:29       10:08:54              -1 days +23:16:26
8:07:15       8:07:53               00:00:38

When Time1 and Time2 are in different hours,I am getting result as"-1 days +" .My desired output for First two values are given below:

Time1         Time2                 Difference
8:59:45       9:27:30               00:27:45
9:52:29       10:08:54              00:16:26

How can I get this output in Pandas?

Both time values are in 'datetime64[ns]' dtype.

EdChum
  • 376,765
  • 198
  • 813
  • 562
Nithin Das
  • 364
  • 1
  • 5
  • 14
  • I don't have your problem. Can you perhaps provide a way to generate the dataframe that reproduces the problem? Also, do you care about the format of the Difference, or would a string work? – IanS Oct 30 '15 at 10:34
  • I think this is a duplicate of this: http://stackoverflow.com/questions/29907529/add-24-hours-to-a-negative-time-difference-in-python – EdChum Oct 30 '15 at 10:39
  • 1
    Or didn't you just do `Time1 - Time2` instead of `Time2 - Time1`? Otherwise [this question](http://stackoverflow.com/questions/18433142/string-formatting-of-timedeltas-in-pandas) might help. – IanS Oct 30 '15 at 10:41
  • @Ian: No, I tried Time2-Time1 to the entire column. Iam getting the same kind of results wherever Time2 andTime1 has different hours. The reference question you suggested is not helping. – Nithin Das Oct 30 '15 at 10:54
  • Can you post your code? – IanS Oct 30 '15 at 10:56

2 Answers2

4

The issue is not that time1 and time2 are in different hours, it's that time2 is before time1 so time2-time1 is negative, and this is how negative timedeltas are stored. If you just want the difference in minutes as a negative number, you could extract the minutes before calculating the difference:

(df.Time1.dt.minute- df.Time2.dt.minute)
maxymoo
  • 35,286
  • 11
  • 92
  • 119
  • 4
    This is not precise. `df.Time.dt.minute` returns the minute component of the `datetime.time` object. So for instance if `time1` is `datetime.time(09,56,36)`, the value of minute is `56`. Now, if the other time you're going to subtract is one hour away or more, the subtraction won't return the right result. For instance `datetime.time(8, 56, 6).minute - datetime.time(7, 56, 6).minute` return `0` minutes difference, whereas the expected results is `60`. – Gianluca May 24 '16 at 19:43
2

I was not able to reproduce the issue using pandas 17.1:

import pandas as pd

d = {
    "start_time": [
        "8:59:45",
        "9:52:29",
        "8:07:15"
    ],
    "end_time": [
        "9:27:30",
        "10:08:54",
        "8:07:53"
    ]
}

from datetime import  datetime
df = pd.DataFrame(data=d)
df['start_time'] = pd.to_datetime(df['start_time'])
df['end_time'] = pd.to_datetime(df['end_time'])

df.end_time - df.start_time

0   00:27:45
1   00:16:25
2   00:00:38
dtype: timedelta64[ns]
Kamil Sindi
  • 21,782
  • 19
  • 96
  • 120