0

Please refer to the image below on the end of this post.

For some reason, the column in red (AVERAGE_TIME) has a time format that displays the micro/milliseconds. Whilst the column in yellow (TOTAL_TIME) doesn't show this information.

I've converted both columns to strings using the same command highlighted by the yellow/red arrows.

How can I have the red column, in the same format as the yellow column? So basically without the micro/milliseconds.

Screenshot

  • have you tried datetime's [strftime](https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior)? – gavin Dec 16 '20 at 19:14
  • Hi @gavin, I didn't try strftime, but I did try strptime(). I believe I'm supposed to use the latter since the columns are strings (right?). It's strange, because when I used strptime() even with a '%H%M%S' format, it returned me yyyy/mm/dd... – Kenneth De Coster Dec 16 '20 at 22:26

1 Answers1

1

You might have to do add some extra steps to get the exact format you want your durations in. But, it wouldn't be anything overly complex. For example, if you don't want microseconds, just add a .seconds to the delta object.

>>> td = timedelta(seconds=71113.45)
>>> td
datetime.timedelta(seconds=71113, microseconds=450000)
>>> td.seconds
71113

But, you may have to convert these to minutes on your own. Also, check out this thread which focuses on formatting timedelta in str, if you choose to head that way.


Alternatively, you can round off the seconds values before you pass them through timedelta directly.

str(timedelta(seconds=round(df_sb.iloc[i, 5])))
gavin
  • 793
  • 1
  • 7
  • 19
  • Hi gavin, the alternative actually resolved it for me. Instead of allowing float values, I just round up the results to integer values before transforming them to hh:mm:ss. Thanks for the help! – Kenneth De Coster Dec 17 '20 at 12:36