How do I round datetime column to nearest quarter hour

64,452

This looks a little nicer

column.dt. allows the datetime functions for datetime columns, like column.str. does for string-like columns

datetime-like properties API reference

import pandas as pd

# test df
df = pd.DataFrame([{'old_column':pd.Timestamp('2015-07-18 13:53:33.280')}])

df['new_column'] = df['old_column'].dt.round('15min')

df
Share:
64,452
sfactor
Author by

sfactor

Dreamer, Analyst, Engineer, Programmer, Photographer.

Updated on July 08, 2022

Comments

  • sfactor
    sfactor almost 2 years

    I have loaded a data file into a Python pandas dataframe. I has a datetime column of the format 2015-07-18 13:53:33.280.

    What I need to do is create a new column that rounds this out to its nearest quarter hour. So, the date above will be rounded to 2015-07-18 13:45:00.000.

    How do I do this in pandas? I tried using the solution from here, but get an 'Series' object has no attribute 'year' error.