Python: 时间戳秒后四舍五入小数位

Python: Round decimal places after seconds in timestamp

我有这个系列:

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

我该怎么做?到时间戳?

如果 .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]