Pandas round DateTime to day

Being able to round a DateTime object in Python to the nearest day 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 day?

In order to round a DateTime object to the nearest day, 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 day you will need to use round("D").

Python Pandas round DateTime to day code

Pandas round DateTime to day

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

import pandas as pd

df = pd.DataFrame(
	columns=["datetime"],
	data=pd.date_range("1/1/2022 09:00:00", periods=6, freq="H"))

df["day_datetime"] = df["datetime"].dt.round("D")

"""
Output:

datetime                day_datetime
0 2022-01-01 09:00:00   2022-01-01
1 2022-01-01 10:00:00   2022-01-01
2 2022-01-01 11:00:00   2022-01-01
3 2022-01-01 12:00:00   2022-01-02
4 2022-01-01 13:00:00   2022-01-02
5 2022-01-01 14:00:00   2022-01-02
"""

Pandas round DateTime to day and return as integer

You may also want to return the day as an integer instead of a DateTime object, 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 09:00:00", periods=6, freq="H"))

df["day_integer"] = df["datetime"].dt.round("D").dt.day

"""
Output:

datetime                 day_integer
0 2022-01-01 09:00:00    1
1 2022-01-01 10:00:00    1
2 2022-01-01 11:00:00    1
3 2022-01-01 12:00:00    2
4 2022-01-01 13:00:00    2
5 2022-01-01 14:00:00    2
"""

Round Pandas DateTime down to nearest day

The round operation from Pandas rounds to the nearest day, but what if you want to always round down to the nearest day? 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 09:00:00", periods=6, freq="H"))

df["round_down_day_datetime"] = df["datetime"].dt.floor("D")
df["round_down_day_integer"] = df["datetime"].dt.floor("D").dt.day

"""
Output:

datetime                 round_down_day_datetime   round_down_day_integer
0 2022-01-01 09:00:00    2022-01-01                1
1 2022-01-01 10:00:00    2022-01-01                1
2 2022-01-01 11:00:00    2022-01-01                1
3 2022-01-01 12:00:00    2022-01-01                1
4 2022-01-01 13:00:00    2022-01-01                1
5 2022-01-01 14:00:00    2022-01-01                1
"""

Round Pandas DateTime up to nearest day

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

import pandas as pd

df = pd.DataFrame(
	columns=["datetime"],
	data=pd.date_range("1/1/2022 09:00:00", periods=6, freq="H"))

df["round_up_day_datetime"] = df["datetime"].dt.ceil("D")
df["round_up_day_integer"] = df["datetime"].dt.ceil("D").dt.day

"""
Output:

datetime                 round_up_day_datetime    round_up_day_integer
0 2022-01-01 09:00:00    2022-01-02               2
1 2022-01-01 10:00:00    2022-01-02               2
2 2022-01-01 11:00:00    2022-01-02               2
3 2022-01-01 12:00:00    2022-01-02               2
4 2022-01-01 13:00:00    2022-01-02               2
5 2022-01-01 14:00:00    2022-01-02               2
"""

Round Pandas DateTime to nearest week
Round Pandas DateTime to start or end of month
Round Python DateTime to week number
Round Python DateTime to month number
Convert Pandas DateTime to date
Round Python DateTime to year

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