【问题标题】:Python: Round decimal places after seconds in timestampPython:时间戳中秒后的小数位四舍五入
【发布时间】:2021-07-24 13:08:05
【问题描述】:

我有这个系列:

30478   2021-06-15 16:23:04.388
30479   2021-06-15 16:23:19.734
30480   2021-06-15 16:23:35.239
30481   2021-06-15 16:23:50.721
30482   2021-06-15 16:24:06.056

最初,我想通过以下方式四舍五入:

df["timestamp"].dt.round('1s')

>>
30478   2021-06-15 16:23:04
30479   2021-06-15 16:23:20
30480   2021-06-15 16:23:35
30481   2021-06-15 16:23:51
30482   2021-06-15 16:24:06

但是,我正在尝试将此数据集合并到另一个更高频率的数据集。所以我想将时间戳的小数位四舍五入。

示例:2021-06-15 16:23:04.388 将变为 2021-06-15 16:23:04.380

我该怎么做?到时间戳?

【问题讨论】:

    标签: python pandas timestamp rounding series


    【解决方案1】:

    如果.388 应该变为.380,我们可以dt.floor 精确到10 毫秒:

    df['timestamp'].dt.floor('10ms')
    

    df['timestamp'].dt.floor('10L')
    
    30478   2021-06-15 16:23:04.380
    30479   2021-06-15 16:23:19.730
    30480   2021-06-15 16:23:35.230
    30481   2021-06-15 16:23:50.720
    30482   2021-06-15 16:24:06.050
    Name: timestamp, dtype: datetime64[ns]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-03
      • 2021-12-11
      • 1970-01-01
      • 2017-12-26
      • 2018-05-27
      • 2013-12-21
      • 2014-02-10
      • 1970-01-01
      相关资源
      最近更新 更多