Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round DateTime using Pandas

I want to round a DateTime feature such a way that it gets converted to the start value of every 10 minute time interval. Example below shows the desired result:

     Actual Time              |        Round Time(within 10 min interval)


  2016-01-01 05:47:46                      2016-01-01 05:40:00
  2016-01-01 05:49:58                      2016-01-01 05:40:00
  2016-01-01 05:50:01                      2016-01-01 05:50:00
  2016-01-01 05:59:58                      2016-01-01 05:50:00  

Code below, rounds it to nearest 10 minute interval instead of always giving the floor value.

df['DateTime'].agg(lambda x : x.round('10min'))

It rounds the DateTime Feature to the nearest value.

   Actual Time              |        Round Time(within 10 min interval)

  2016-01-01 05:47:46                      2016-01-01 05:50:00
  2016-01-01 05:49:58                      2016-01-01 05:50:00
  2016-01-01 05:50:01                      2016-01-01 05:50:00
  2016-01-01 05:59:58                      2016-01-01 06:00:00  

Round time such a way that it falls near the floor value.

like image 527
Stanger Danger Avatar asked Jan 02 '23 12:01

Stanger Danger


1 Answers

Use Series.dt.floor:

df['Round Time'] = df['DateTime'].dt.floor('10min')
like image 91
jezrael Avatar answered Jan 04 '23 03:01

jezrael