首页 > 解决方案 > 从日期时间差中提取小时与舍入 1 - Pandas DataFrame

问题描述

有没有一个选项可以用小数点(8.4,9.2)提取小时的时差。当我使用

(arm['EndTime']-arm['StartTime']).astype('timedelta64[h]')

结果是 8.0 而不是 8.4,和 9 而不是 9.2 结果自动四舍五入为 0...我尝试使用以下代码

(arm['EndTime']-arm['StartTime']).astype('timedelta64[h]').round(1)

但它产生了相同的结果。

标签: pythonpandasnumpyautomation

解决方案


您可以使用withSeries.dt.total_seconds除以with :3600Series.divSeries.round

(arm['EndTime']-arm['StartTime']).dt.total_seconds().div(3600).round(1)

推荐阅读