如何正确地减去两个 UNIX 时间戳列

How to properly subtract two UNIX timestamp columns

有没有办法在不进行转换的情况下减去两个 UNIX 时间戳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

当然,只需将毫秒时间戳相互减去,然后将结果to_timedelta转换为适当的单位:

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