Python Pandas round DateTime to minute

Pandas round DateTime to minute

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

Stephen Allwright
Stephen Allwright

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

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

Python Pandas round DateTime to minute code

Pandas round DateTime to minute

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

import pandas as pd

df = pd.DataFrame(
	columns=["datetime"],
	data=pd.date_range("1/1/2022 20:30:28", periods=6, freq="s"))

df["minute_datetime"] = df["datetime"].dt.round("min")

"""
Output:

datetime              minute_datetime
0 2022-01-01 20:30:28 2022-01-01 20:30:00
1 2022-01-01 20:30:29 2022-01-01 20:30:00
2 2022-01-01 20:30:30 2022-01-01 20:30:00
3 2022-01-01 20:30:31 2022-01-01 20:31:00
4 2022-01-01 20:30:32 2022-01-01 20:31:00
5 2022-01-01 20:30:33 2022-01-01 20:31:00
"""

Pandas round DateTime to minute and return as integer

You may also want to return the minute 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:30:28", periods=6, freq="s"))

df["minute_integer"] = df["datetime"].dt.round("min").dt.minute

"""
Output:

datetime                           minute_integer
0 2022-01-01 20:30:28              30
1 2022-01-01 20:30:29              30
2 2022-01-01 20:30:30              30
3 2022-01-01 20:30:31              31
4 2022-01-01 20:30:32              31
5 2022-01-01 20:30:33              31
"""

Round Pandas DateTime down to nearest minute

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

df["round_down_minute_datetime"] = df["datetime"].dt.floor("min")
df["round_down_minute_integer"] = df["datetime"].dt.floor("min").dt.minute

"""
Output:

datetime                     round_down_minute_datetime   round_down_minute_integer
0 2022-01-01 20:30:28        2022-01-01 20:30:00                         30
1 2022-01-01 20:30:29        2022-01-01 20:30:00                         30
2 2022-01-01 20:30:30        2022-01-01 20:30:00                         30
3 2022-01-01 20:30:31        2022-01-01 20:30:00                         30
4 2022-01-01 20:30:32        2022-01-01 20:30:00                         30
5 2022-01-01 20:30:33        2022-01-01 20:30:00                         30
"""

Round Pandas DateTime up to nearest minute

Likewise, if you want to always round up the nearest minute 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_minute_datetime"] = df["datetime"].dt.ceil("min")
df["round_up_minute_integer"] = df["datetime"].dt.ceil("min").dt.minute

"""
Output:

datetime                   round_up_minute_datetime     round_up_minute_integer
0 2022-01-01 20:30:28      2022-01-01 20:31:00                       31
1 2022-01-01 20:30:29      2022-01-01 20:31:00                       31
2 2022-01-01 20:30:30      2022-01-01 20:31:00                       31
3 2022-01-01 20:30:31      2022-01-01 20:31:00                       31
4 2022-01-01 20:30:32      2022-01-01 20:31:00                       31
5 2022-01-01 20:30:33      2022-01-01 20:31:00                       31
"""

Round Pandas DateTime to hour
Round Pandas DateTime to day
Round Pandas DateTime to seconds
Round Python DateTime to quarter hour

References

Pandas round documentation
Pandas floor documentation
Pandas 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.