0

I have a dataset which stores durations like 3 hours and 7 minutes in the format of, 3.11 as a string. I want to convert the column containing these values into datetime in a way that I get: 03:07.

When I do:

df["ConnectedDuration"] = pd.to_datetime(df['ConnectedDuration'])

I get: 1970-01-01 00:00:00.000000003 which is obviousely not what I want.

When I do:

df["ConnectedDuration"] = pd.to_datetime(df['ConnectedDuration'], format='%H:%M')

I get the following error: ValueError: time data '3' does not match format '%H:%M' (match)

Any help is highly appreciated

1 Answer 1

3

You want to convert this values to timedelta instead of datetime. Thus you should use the pd.to_timedelta method, like:

pd.to_timedelta(df["ConnectedDuration"].astype('float'), unit='h')
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! I will accept your answer once it's possible.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.