> ## 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 To Nearest Hour in Pandas
- URL: https://datascientyst.com/how-to-round-to-nearest-hour-in-pandas/
- Published: 2023-08-27T15:01:01.000Z
- Updated: 2023-08-27T15:01:01.000Z
- Author: John D K
- Tags: Time Series

To round to closest hour in Pandas datetime column we can several options:

**(1) Round to nearest hour**

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

```

**(2) floor to closest hour**

```python
df['date'].dt.floor('h')

```

**(3) ceil to closest hour**

```python
df['date'].dt.ceil('h')

```

The image below show the results of 3 options: ![](https://datascientyst.com/content/images/2023/08/how-to-round-to-nearest-hour-in-pandas.png)

Let's cover the cases in examples.

## Example

Suppose we have DataFrame with data:

```python
import pandas as pd

dates = ['2023-08-27 17:45',
     	'2023-08-27 19:15',
     	'2023-08-27 20:07',
     	'2023-08-27 23:55',]
df = pd.DataFrame({'date': dates})

```

result:

|   | date                |
| - | ------------------- |
| 0 | 2023-08-27 17:45:00 |
| 1 | 2023-08-27 19:15:00 |
| 2 | 2023-08-27 20:07:00 |
| 3 | 2023-08-27 23:55:00 |

Note: if you need convert string to datetime column by:

```python
df['date'] = pd.to_datetime(df['date'])

```

## Round to closest hour

First we will try to round to closest hour:

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

```

This will give us the hour as integer:

```
0	18
1	19
2	20
3 	0
Name: date, dtype: int32

```

## Floor to nearest hour

Instead of rounding we can floor time to hour. Operation floor means:

> rounds a number DOWN to the nearest integer, if necessary, and returns the result.

```python
df['date'].dt.floor('h').dt.hour

```

This will give us the hour as integer:

```
0	17
1	19
2	20
3	23
Name: date, dtype: int32

```

As you can notice two results differ from the first case.

## Ceil to nearest hour

Instead of rounding we can also ceil time to hour. Operation ceil means:

> rounds a number UP to the nearest integer, if necessary, and returns the result.

```python
df['date'].dt.ceil('h').dt.hour

```

This will give us the hour as integer:

```
0	18
1	20
2	21
3 	0
Name: date, dtype: int32

```

Again we have different results.

## Resources

- [pandas.Series.dt.floor](https://pandas.pydata.org/docs/reference/api/pandas.Series.dt.floor.html?ref=datascientyst.com)
- [pandas.Series.dt.round](https://pandas.pydata.org/docs/reference/api/pandas.Series.dt.round.html?ref=datascientyst.com)
- [pandas.Series.dt.ceil](https://pandas.pydata.org/docs/reference/api/pandas.Series.dt.ceil.html?ref=datascientyst.com)
- [How to Round Time to the Nearest Quarter or Hour in Pandas?](https://datascientyst.com/how-to-round-time-to-the-nearest-quarter-or-hour-in-pandas/)