Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to divide pandas date time column in half hourly interval

Tags:

python

pandas

I have pandas dataframe like following

  date                      value     
  2018-02-12 17:30:00       23
  2018-02-12 17:34:00       45
  2018-02-12 17:36:00       23
  2018-02-12 17:45:00       56
  2018-02-12 18:37:00       54

Desired pandas dataframe

  date                      value        half_hourly_bucket  
  2018-02-12 17:30:00       23           17:30-17:59
  2018-02-12 17:34:00       45           17:30-17:59
  2018-02-12 17:36:00       23           17:30-17:59
  2018-02-12 17:45:00       56           17:30-17:59
  2018-02-12 18:37:00       54           18:30-18:59

Like wise I have data for all 24 hours. I do not want to use if else loop for 48 times. Is there any better way of doing this in pandas?

like image 614
Neil Avatar asked Aug 24 '18 06:08

Neil


1 Answers

I believe need Series.dt.floor with strftime and add 29Min by Timedeltas:

print (df)
                 date  value
0 2018-02-12 18:00:00     23 <-changed values
1 2018-02-12 17:34:00     45
2 2018-02-12 17:36:00     23
3 2018-02-12 17:45:00     56
4 2018-02-12 18:37:00     54

s = df['date'].dt.floor('30T')
s1 = s.dt.strftime('%H:%M') + '-' + (s + pd.Timedelta(29 * 60, unit='s')).dt.strftime('%H:%M')
print (s1)
0    18:00-18:29
1    17:30-17:59
2    17:30-17:59
3    17:30-17:59
4    18:30-18:59
Name: date, dtype: object
like image 57
jezrael Avatar answered Oct 23 '22 04:10

jezrael