Pandas DateTime round to hour

Being able to round a DateTime object in Python to the nearest hour can be extremely helpful for feature engineering. In this post, I will walk through how to do this simply in multiple variations.

How does Pandas round to the nearest hour?

In order to round a DateTime object to the nearest hour, you need to use the round operation from Pandas on the DateTime column and specify the frequency that you want to use. For rounding to the nearest hour you will need to use round("H").

Python Pandas DateTime round to hour code

Pandas DateTime round to hour

Below is a simple example of how you can round to the nearest hour and return this as a DateTime object.

import pandas as pd

df = pd.DataFrame(
	columns=["datetime"],
	data=pd.date_range("1/1/2022 20:26:00", periods=10, freq="min"))

df["hour_datetime"] = df["datetime"].dt.round("H")

"""
Output:

datetime              hour_datetime
0 2022-01-01 20:26:00 2022-01-01 20:00:00
1 2022-01-01 20:27:00 2022-01-01 20:00:00
2 2022-01-01 20:28:00 2022-01-01 20:00:00
3 2022-01-01 20:29:00 2022-01-01 20:00:00
4 2022-01-01 20:30:00 2022-01-01 20:00:00
5 2022-01-01 20:31:00 2022-01-01 21:00:00
6 2022-01-01 20:32:00 2022-01-01 21:00:00
7 2022-01-01 20:33:00 2022-01-01 21:00:00
8 2022-01-01 20:34:00 2022-01-01 21:00:00
9 2022-01-01 20:35:00 2022-01-01 21:00:00
"""

Pandas DateTime round to hour and return as integer

You may also want to return the hour not as a DateTime object but as an integer instead, this is possible with just a small addition to the previous example.

import pandas as pd

df = pd.DataFrame(
	columns=["datetime"],
	data=pd.date_range("1/1/2022 20:26:00", periods=10, freq="min"))

df["hour_integer"] = df["datetime"].dt.round("H").dt.hour

"""
Output:

datetime                          hour_integer
0 2022-01-01 20:26:00             20
1 2022-01-01 20:27:00             20
2 2022-01-01 20:28:00             20
3 2022-01-01 20:29:00             20
4 2022-01-01 20:30:00             20
5 2022-01-01 20:31:00             21
6 2022-01-01 20:32:00             21
7 2022-01-01 20:33:00             21
8 2022-01-01 20:34:00             21
9 2022-01-01 20:35:00             21
"""

Round Pandas DateTime down to nearest hour

The round operation rounds to the nearest hour, but what if you want to always round down to the nearest hour? Well, for this you need to use the floor operation.

import pandas as pd

df = pd.DataFrame(
	columns=["datetime"],
	data=pd.date_range("1/1/2022 20:26:00", periods=10, freq="min"))

df["round_down_hour_datetime"] = df["datetime"].dt.floor("H")
df["round_down_hour_integer"] = df["datetime"].dt.floor("H").dt.hour

"""
Output:

datetime                   round_down_hour_datetime      round_down_hour_integer
0 2022-01-01 20:26:00      2022-01-01 20:00:00                       20
1 2022-01-01 20:27:00      2022-01-01 20:00:00                       20
2 2022-01-01 20:28:00      2022-01-01 20:00:00                       20
3 2022-01-01 20:29:00      2022-01-01 20:00:00                       20
4 2022-01-01 20:30:00      2022-01-01 20:00:00                       20
5 2022-01-01 20:31:00      2022-01-01 20:00:00                       20
6 2022-01-01 20:32:00      2022-01-01 20:00:00                       20
7 2022-01-01 20:33:00      2022-01-01 20:00:00                       20
8 2022-01-01 20:34:00      2022-01-01 20:00:00                       20
9 2022-01-01 20:35:00      2022-01-01 20:00:00                       20
"""

Round Pandas DateTime up to nearest hour

Likewise, if you want to always round up the nearest hour you need to use the ceil operation.

import pandas as pd

df = pd.DataFrame(
	columns=["datetime"],
	data=pd.date_range("1/1/2022 20:26:00", periods=10, freq="min"))

df["round_up_hour_datetime"] = df["datetime"].dt.ceil("H")
df["round_up_hour_integer"] = df["datetime"].dt.ceil("H").dt.hour

"""
Output:

datetime                 round_up_hour_datetime        round_up_hour_integer
0 2022-01-01 20:26:00    2022-01-01 21:00:00                     21
1 2022-01-01 20:27:00    2022-01-01 21:00:00                     21
2 2022-01-01 20:28:00    2022-01-01 21:00:00                     21
3 2022-01-01 20:29:00    2022-01-01 21:00:00                     21
4 2022-01-01 20:30:00    2022-01-01 21:00:00                     21
5 2022-01-01 20:31:00    2022-01-01 21:00:00                     21
6 2022-01-01 20:32:00    2022-01-01 21:00:00                     21
7 2022-01-01 20:33:00    2022-01-01 21:00:00                     21
8 2022-01-01 20:34:00    2022-01-01 21:00:00                     21
9 2022-01-01 20:35:00    2022-01-01 21:00:00                     21
"""

Round Pandas DateTime to minute
Round Python DateTime to nearest 15 minutes
Round Pandas DateTime to day
Round Pandas DateTime to seconds

References

Pandas round documentation
Pandas floor documentation
Pandas ceil documentation

Stephen Allwright

Stephen Allwright

I'm a Data Scientist currently working for Oda, an online grocery retailer, in Oslo, Norway. These posts are my way of sharing some of the tips and tricks I've picked up along the way.
Oslo, Norway