> ## Content Index
> Fetch the complete content index at: https://datascientyst.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to Round Time to the Nearest Quarter or Hour in Pandas?
- URL: https://datascientyst.com/how-to-round-time-to-the-nearest-quarter-or-hour-in-pandas/
- Published: 2023-04-02T20:38:19.000Z
- Updated: 2023-04-02T20:38:19.000Z
- Author: John D K
- Tags: Time Series

To round a **datetime column to the nearest quarter, minute or hour in Pandas**, we can use the method: `dt.round()`.

![](https://datascientyst.com/content/images/2023/04/round-time-to-the-nearest-quarter-or-hour-in-pandas.png)

## round datetime column to nearest hour

Below you can find an example of rounding to the closest hour in Pandas and Python. We use method `dt.round()` with parameter `H`:

```python
import pandas as pd

dates = ['2023-03-25 11:37:00', '2023-03-25 09:18:00', '2023-03-25 15:23:00']
df = pd.DataFrame({'date': dates})
df['date'] = pd.to_datetime(df['date'])

df['date'].dt.round('H')

```

the result is rounded times to the nearest hour:

```
0   2023-03-25 12:00:00
1   2023-03-25 09:00:00
2   2023-03-25 15:00:00
Name: date, dtype: datetime64[ns]

```

## round datetime to nearest quarter or minutes

We can round to the nearest quarter in Pandas by the same method: `.dt.round('15min')` specifying the interval in minutes. Example of rounding down in Pandas to N minutes:

```python
import pandas as pd

dates = ['2023-03-25 11:37:00', '2023-03-25 09:18:00', '2023-03-25 15:23:00']
df = pd.DataFrame({'date': dates})
df['date'] = pd.to_datetime(df['date'])

df['date'].dt.round('15min')

```

result of rounding to quarter is:

```
0   2023-03-25 11:30:00
1   2023-03-25 09:15:00
2   2023-03-25 15:30:00
Name: date, dtype: datetime64[ns]

```

In the next section you can find a link to all possible frequency values.

## datetime - round vs floor

Finally let's see what is the difference between Pandas methods:

- round
- floor

```python
df['rounded_date'] = df['date'].dt.round('H')

df['floored_date'] = df['date'].dt.floor('H')

```

You can find the result below:

|   | date                | rounded\_date       | floored\_date       |
| - | ------------------- | ------------------- | ------------------- |
| 0 | 2023-03-25 11:37:00 | 2023-03-25 12:00:00 | 2023-03-25 11:00:00 |
| 1 | 2023-03-25 09:18:00 | 2023-03-25 09:00:00 | 2023-03-25 09:00:00 |
| 2 | 2023-03-25 15:23:00 | 2023-03-25 15:00:00 | 2023-03-25 15:00:00 |

So difference is in the first row where:

- `12:00:00` \- round the datetime column to the nearest hour
- `11:00:00` \- floor the datetime column to the nearest hour

## Resources

- [pandas.Series.dt.round](https://pandas.pydata.org/docs/reference/api/pandas.Series.dt.round.html?ref=datascientyst.com)
- [pandas.to\_datetime](https://pandas.pydata.org/docs/reference/api/pandas.to%5Fdatetime.html?highlight=to%5Fdatetime&ref=datascientyst.com)
- [Time series / date functionality](https://pandas.pydata.org/docs/user%5Fguide/timeseries.html?ref=datascientyst.com)
- [Offset/frequency aliases](https://pandas.pydata.org/docs/user%5Fguide/timeseries.html?ref=datascientyst.com#offset-aliases) \- for a list of possible freq values
- [pandas.Series.dt.floor](https://pandas.pydata.org/docs/reference/api/pandas.Series.dt.floor.html?ref=datascientyst.com)