Home > Back-end >  How to compute elapsed time between two dates in a CSV file using Python?
How to compute elapsed time between two dates in a CSV file using Python?

Time:09-28

I just want to ask you how can I compute the elapsed time between two date using Python.

I try to do it using some methods found on GeeksForGeeks but I can't get it.

You can find my code here :

 import csv
 import pandas as pd
 from datetime import date

 data = pd.read_csv("./dates.csv)
 first_t = date(data.departure_ts)
 later_t = date(data.arrival_ts)


 diff = later_t - first_t
 print(diff)

Here you can find some exemple of data that I use

departure_ts            arrival_ts
2017-10-13 14:00:00 00  2017-10-13 20:10:00 00
2017-10-13 13:05:00 00  2017-10-14 06:55:00 00
2017-10-13 13:27:00 00  2017-10-14 21:24:00 00
2017-10-13 13:27:00 00  2017-10-14 11:02:00 00

CodePudding user response:

Convert your date strings to pandas.Timestamp using pandas.to_datetime, after which you can directly subtract:

delta = pd.to_datetime(df["arrival_ts"])-pd.to_datetime(df["departure_ts"])

>>> delta
0   0 days 06:10:00
1   0 days 17:50:00
2   1 days 07:57:00
3   0 days 21:35:00
dtype: timedelta64[ns]
  • Related