Substracting time values from csv file by python

50 views Asked by At

From a csv file I am reading a time series in this format 23:03:00 using pandas. The python code reads it as a string & can't convert it to integer or float to subtract it from another time step.

for example:

df = pd.read_csv('test.csv', sep=";")

x=df.iloc[1,2]

y=df.iloc[1,2]

dif=y-x
1

There are 1 answers

2
Mougnou On BEST ANSWER

You can use strptime from datetime library here is an exemple :

from datetime import datetime

date_string1 = "23:03:00"
date_string2 = "23:01:00"

date_object1 = datetime.strptime(date_string1, "%H:%M:%S")
date_object2 = datetime.strptime(date_string2, "%H:%M:%S")

difference = date_object1 - date_object2

print(difference) 

The second parameter of strptime is the format of your date.