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

Python how to round Timestamp object to the previous full hour

Hi have a list of timestamp objects:

Timestamp('2021-07-07 10:00:03'), Timestamp('2021-07-07 10:02:13'), Timestamp('2021-03-07 12:40:24')

And I want to round each element at the hour level, to get:

Timestamp('2021-07-07 10:00:00'), Timestamp('2021-07-07 10:00:00'), Timestamp('2021-03-07 12:00:00')

The type of each element is

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

Pandas Timestamp (pandas._libs.tslibs.timestamps.Timestamp)

.
What is the best way to do so?

>Solution :

Given

>>> df 
                 time
0 2021-07-07 10:00:03
1 2021-07-07 10:02:13
2 2021-03-07 12:40:24

Use

>>> df['time'] = df['time'].dt.floor('1h')
>>> df 
                 time
0 2021-07-07 10:00:00
1 2021-07-07 10:00:00
2 2021-03-07 12:00:00
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