Python Pandas round DateTime to nearest second

Pandas round DateTime to seconds

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

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

Python Pandas round DateTime to nearest seconds code

Pandas round DateTime to seconds

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

import pandas as pd

df = pd.DataFrame(
	columns=["datetime"],
	data=pd.to_datetime([
		"1/1/2022 09:00:00.4",
		"1/1/2022 09:00:00.5",
		"1/1/2022 09:00:00.6",
		"1/1/2022 09:00:00.7"]))

df["second_datetime"] = df["datetime"].dt.round("S")

"""
Output:

datetime                     second_datetime
0 2022-01-01 09:00:00.400    2022-01-01 09:00:00
1 2022-01-01 09:00:00.500    2022-01-01 09:00:00
2 2022-01-01 09:00:00.600    2022-01-01 09:00:01
3 2022-01-01 09:00:00.700    2022-01-01 09:00:01
"""

Pandas round DateTime to seconds and return as integer

You may also want to return the second 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.to_datetime([
		"1/1/2022 09:00:00.4",
		"1/1/2022 09:00:00.5",
		"1/1/2022 09:00:00.6",
		"1/1/2022 09:00:00.7"]))

df["second_integer"] = df["datetime"].dt.round("S").dt.second

"""
Output:

datetime                     second_integer
0 2022-01-01 09:00:00.400    0
1 2022-01-01 09:00:00.500    0
2 2022-01-01 09:00:00.600    1
3 2022-01-01 09:00:00.700    1
"""

Round Pandas DateTime down to nearest second

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

import pandas as pd

df = pd.DataFrame(
	columns=["datetime"],
	data=pd.to_datetime([
		"1/1/2022 09:00:00.4",
		"1/1/2022 09:00:00.5",
		"1/1/2022 09:00:00.6",
		"1/1/2022 09:00:00.7"]))

df["round_down_second_datetime"] = df["datetime"].dt.floor("S")
df["round_down_second_integer"] = df["datetime"].dt.floor("S").dt.second

"""
Output:

datetime                     round_down_second_datetime    round_down_second_integer
0 2022-01-01 09:00:00.400    2022-01-01 09:00:00           0
1 2022-01-01 09:00:00.500    2022-01-01 09:00:00           0
2 2022-01-01 09:00:00.600    2022-01-01 09:00:00           0
3 2022-01-01 09:00:00.700    2022-01-01 09:00:00           0
"""

Round Pandas DateTime up to nearest second

Likewise, if you want to always round up the nearest second 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_second_datetime"] = df["datetime"].dt.ceil("S")
df["round_up_second_integer"] = df["datetime"].dt.ceil("S").dt.second

"""
Output:

datetime                    round_up_second_datetime   round_up_second_integer
0 2022-01-01 09:00:00.400   2022-01-01 09:00:01        1
1 2022-01-01 09:00:00.500   2022-01-01 09:00:01        1
2 2022-01-01 09:00:00.600   2022-01-01 09:00:01        1
3 2022-01-01 09:00:00.700   2022-01-01 09:00:01        1
"""

Python round to nearest hour
Python round to nearest minute
Python round to nearest quarter hour
Python round to nearest day

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.