简体   繁体   English

从 Python 中的时间格式中删除毫秒

[英]Remove milliseconds from time format in Python

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.出于某种原因,红色列 ( AVERAGE_TIME ) 具有显示微/毫秒的时间格式。 Whilst the column in yellow ( TOTAL_TIME ) doesn't show this information.虽然黄色列 ( TOTAL_TIME ) 不显示此信息。

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.所以基本上没有微/毫秒。

截屏

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.例如,如果您不想要微秒,只需将.seconds添加到增量 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.另外,如果您选择采用这种方式,请查看此线程,该线程专注于格式化 str 中的timedelta


Alternatively, you can round off the seconds values before you pass them through timedelta directly.或者,您可以在直接通过 timedelta 之前将秒值round

str(timedelta(seconds=round(df_sb.iloc[i, 5])))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM