0

I would like to transform this column (format int64) to a minutes format to do calculation

Duration(min)
10
30
5
15
5

I tried this:

df['Duration(min)'] = pd.to_datetime(df['Duration(min)'], format='%H%M')

But I have ValueError: time data '5' does not match format '%H%M' (match)

3
  • These values are very small to create datetime. There should be at least 2 digits Commented Oct 31, 2020 at 18:36
  • Can you detail your use case a little more? Why do you need a date format? Commented Oct 31, 2020 at 18:38
  • It's a duration of an intervention in a case of maintenance Commented Oct 31, 2020 at 18:41

3 Answers 3

3

You need to use pd.to_timedelta here.

pd.to_timedelta(df['Duration(min)'], unit='min')

0   0 days 00:10:00
1   0 days 00:30:00
2   0 days 00:05:00
3   0 days 00:15:00
4   0 days 00:05:00
Name: Duration(min), dtype: timedelta64[ns]

unit -> ‘m’ / ‘minute’ / ‘min’ / ‘minutes’ / ‘T’ all are shorthand for minutes.

Sign up to request clarification or add additional context in comments.

Comments

3

Using pd.to_datetime with strftime -

pd.to_datetime(df.Duration(min), unit='m').dt.strftime('%H:%M')

Output-

0    00:10
1    00:30
2    00:05
3    00:15
4    00:05
dtype: object

Comments

0

Why %H%M when all you have is minutes? Try format='%M'.

Comments

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.