Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to proprely subtract two UNIX timestamps 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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading