Python round DateTime to nearest 15 minutes or quarter hour

Python round time to nearest 15 minutes

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

Stephen Allwright
Stephen Allwright

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

How do you round to the nearest quarter hour from a DateTime object in Python?

Python doesn’t have built-in functionality for rounding a DateTime to the nearest quarter hour, as it does for seconds, minutes, and hours. Therefore, in order to round to the nearest 15 minutes, we have to create a custom function to do this.

Python round time to nearest 15 minutes

In order to round to the nearest 15 minutes we need to use a combination of round and timedelta. In this code example we combine these operations into a simple function whose inputs are the DateTime that will be rounded, and the time window to round towards, which in our case will be 15 minutes.

from datetime import datetime, timedelta

def round_dt(dt, delta):
    return datetime.min + round((dt - datetime.min) / delta) * delta

delta = timedelta(minutes=15)

dt = datetime(2022, 9, 1, 14, 28, 0)
print(round_dt(dt,delta))

dt = datetime(2022, 9, 1, 14, 20, 0)
print(round_dt(dt,delta))

"""
Output:

2022-09-01 14:30:00
2022-09-01 14:15:00
"""

Python round DateTime up to nearest 15 minutes using ceil

The previous example uses round which will round to the nearest 15 minutes, whether that is up or down. However, we may always want to round upwards. This requires only a tiny change to the example we saw above, where we use math.ceil instead of round.

from datetime import datetime, timedelta
import math

def round_dt(dt, delta):
    return datetime.min + math.ceil((dt - datetime.min) / delta) * delta

delta = timedelta(minutes=15)

dt = datetime(2022, 9, 1, 14, 28, 0)
print(round_dt(dt,delta))

dt = datetime(2022, 9, 1, 14, 20, 0)
print(round_dt(dt,delta))

"""
Output:

2022-09-01 14:30:00
2022-09-01 14:30:00
"""

Python round DateTime down to nearest 15 minutes using floor

If you want to always round down to the nearest 15 minutes then we need to use math.floor instead of round.

from datetime import datetime, timedelta
import math

def round_dt(dt, delta):
    return datetime.min + math.floor((dt - datetime.min) / delta) * delta

delta = timedelta(minutes=15)

dt = datetime(2022, 9, 1, 14, 28, 0)
print(round_dt(dt,delta))

dt = datetime(2022, 9, 1, 14, 20, 0)
print(round_dt(dt,delta))

"""
Output:

2022-09-01 14:15:00
2022-09-01 14:15:00
"""


Pandas DateTime round to hour
Pandas round DateTime to minute
Pandas round DateTime to day
Pandas round DateTime to seconds

References

Python datetime documentation
Python floor and ceil documentation

Pandas

Stephen Allwright Twitter

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.