Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas/Datetime rounding error (Rounding to 10 seconds)

I have a problem using the round() funktion on a pandas.Series / DataFrame filled with Timestamps.

Code example to round to the nearest 10-second mark:

import pandas as pd

date = pd.DataFrame([pd.to_datetime('2022-03-02 06:46:05'), pd.to_datetime('2022-03-02 06:46:15'), pd.to_datetime('2022-03-02 06:46:25'), pd.to_datetime('2022-03-02 06:46:35'), pd.to_datetime('2022-03-02 06:46:45'), pd.to_datetime('2022-03-02 06:46:55'), pd.to_datetime('2022-03-02 06:47:05'), pd.to_datetime('2022-03-02 06:47:15'), pd.to_datetime('2022-03-02 06:47:25')])

date[1] = date[0].round('10s')

date

OUT:
    0                     1
0   2022-03-02 06:46:05   2022-03-02 06:46:00
1   2022-03-02 06:46:15   2022-03-02 06:46:20
2   2022-03-02 06:46:25   2022-03-02 06:46:20
3   2022-03-02 06:46:35   2022-03-02 06:46:40
4   2022-03-02 06:46:45   2022-03-02 06:46:40
5   2022-03-02 06:46:55   2022-03-02 06:47:00
6   2022-03-02 06:47:05   2022-03-02 06:47:00
7   2022-03-02 06:47:15   2022-03-02 06:47:20
8   2022-03-02 06:47:25   2022-03-02 06:47:20
dtype: datetime64[ns]

Whenever a Timestamp has a seconds value in [5, 25, 45] the rounded value is set to [0, 20, 40], although it should be set to [10, 30, 50]. Any idea on how to fix this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks in advance!

>Solution :

Use trick – add some small timedelta, because python should round 5 not like expected:

date[1] = date[0].add(pd.Timedelta('1us')).round('10s')
print (date)
                    0                   1
0 2022-03-02 06:46:05 2022-03-02 06:46:10
1 2022-03-02 06:46:15 2022-03-02 06:46:20
2 2022-03-02 06:46:25 2022-03-02 06:46:30
3 2022-03-02 06:46:35 2022-03-02 06:46:40
4 2022-03-02 06:46:45 2022-03-02 06:46:50
5 2022-03-02 06:46:55 2022-03-02 06:47:00
6 2022-03-02 06:47:05 2022-03-02 06:47:10
7 2022-03-02 06:47:15 2022-03-02 06:47:20
8 2022-03-02 06:47:25 2022-03-02 06:47:30
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading