Difference between two timestamps in Pandas
Asked Answered
L

2

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.

Lezlie answered 30/10, 2015 at 10:10 Comment(5)
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?Languorous
I think this is a duplicate of this: #29908029Sabbatarian
Or didn't you just do Time1 - Time2 instead of Time2 - Time1? Otherwise this question might help.Languorous
@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.Lezlie
Can you post your code?Languorous
F
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)
Fidelia answered 5/1, 2016 at 4:18 Comment(1)
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.Bisexual
S
3

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]
Symploce answered 5/1, 2016 at 3:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.