How to convert a string (X hours X minutes X seconds) to datetime in Python?

I have a column that stores time as such, ‘3 hours 10 minutes’. I want to sum the column to get a total amount of hours. How can I convert this string into a timestamp?

Add Comment
1 Answer(s)

To parse string into datetime, you can use datetime.strptime():

>>> from datetime import datetime >>>  >>> datetime.strptime('3 hours 10 minutes', '%H hours %M minutes') datetime.datetime(1900, 1, 1, 3, 10) 

This will return a datetime object, which you can process further.

More info here.

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.