Search code examples
pythonpandasdatetimeepochtimedelta

How to properly subtract two UNIX timestamp columns


Is there a way to subtract two UNIX timestamp without transformation to_datetime() ?

data = {'start_ts':[1612642244163, 1612642238996],
        'end_ts':[1612642535739, 1612642837002]}

df = pd.DataFrame(data)

df["start_ts"] = pd.to_datetime(df["start_ts"], unit="ms")
df["end_ts"] = pd.to_datetime(df["end_ts"], unit="ms")
df["duration"] = df["end_ts"] - df["start"]

    start_ts                |end_ts                  |duration
0   2021-02-06 20:10:44.163 |2021-02-06 20:15:35.739 |0 days 00:04:51.576000
1   2021-02-06 20:10:38.996 |2021-02-06 20:20:37.002 |0 days 00:09:58.006000

Solution

  • sure, just subtract the milliseconds timestamps from each other, then convert the results to_timedelta with the appropriate unit:

    df = pd.DataFrame({'start_ts':[1612642244163, 1612642238996],
                       'end_ts':[1612642535739, 1612642837002]})
    
    df["duration"] = pd.to_timedelta(df["end_ts"] - df["start_ts"], unit='ms')
    
    df
            start_ts         end_ts               duration
    0  1612642244163  1612642535739 0 days 00:04:51.576000
    1  1612642238996  1612642837002 0 days 00:09:58.006000