0

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 Answer 1

0

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.

2
  • Thank you very much for answering my question,. i have one issue when i convert the difference, let's say '00:02:00' to float i got an error; python error: could not convert string to float
    – Ayman M
    Commented Jan 29 at 15:07
  • Try using this difference.total_seconds()
    – Mougnou
    Commented Jan 29 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.