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?
I believe need Series.dt.floor
with strftime
and add 29Min
by Timedelta
s:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With