I have this series:
JavaScript
x
6
1
30478 2021-06-15 16:23:04.388
2
30479 2021-06-15 16:23:19.734
3
30480 2021-06-15 16:23:35.239
4
30481 2021-06-15 16:23:50.721
5
30482 2021-06-15 16:24:06.056
6
Initially, I wanted to round to seconds by doing:
JavaScript
1
9
1
df["timestamp"].dt.round('1s')
2
3
>>
4
30478 2021-06-15 16:23:04
5
30479 2021-06-15 16:23:20
6
30480 2021-06-15 16:23:35
7
30481 2021-06-15 16:23:51
8
30482 2021-06-15 16:24:06
9
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:
JavaScript
1
2
1
df['timestamp'].dt.floor('10ms')
2
or
JavaScript
1
2
1
df['timestamp'].dt.floor('10L')
2
JavaScript
1
7
1
30478 2021-06-15 16:23:04.380
2
30479 2021-06-15 16:23:19.730
3
30480 2021-06-15 16:23:35.230
4
30481 2021-06-15 16:23:50.720
5
30482 2021-06-15 16:24:06.050
6
Name: timestamp, dtype: datetime64[ns]
7