Skip to content
Advertisement

Python: Round decimal places after seconds in timestamp

I have this series:

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

Initially, I wanted to round to seconds by doing:

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

However, I am trying to merge this dataset to another of higher frequency. So I’d like to round the decimal places of the timestamp instead.

Example: 2021-06-15 16:23:04.388 would become 2021-06-15 16:23:04.380

How can I do this? to a timestamp?

Advertisement

Answer

If .388 should become .380 we can dt.floor to the nearest 10 ms with:

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

or

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]
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement