I was trying to round off time to the nearest hour in python in a dataframe.
Suppose if a timestamp is 2017-11-18 0:16
it should come as 2017-11-18 0:00
and 2017-11-18 1:56
should round off as 2017-11-18 2:00
I was trying to round off time to the nearest hour in python in a dataframe.
Suppose if a timestamp is 2017-11-18 0:16
it should come as 2017-11-18 0:00
and 2017-11-18 1:56
should round off as 2017-11-18 2:00
I experimented a bit with jpp but ended up with a different solution as adding one hour at hour 23 crashed the thing.
from datetime import datetime, timedelta
now = datetime.now()
def hour_rounder(t):
# Rounds to nearest hour by adding a timedelta hour if minute >= 30
return (t.replace(second=0, microsecond=0, minute=0, hour=t.hour)
+timedelta(hours=t.minute//30))
print(now)
print(hour_rounder(now))
Returns:
2018-02-22 23:42:43.352133
2018-02-23 00:00:00
This is one way.
from datetime import datetime
now = datetime.now()
def rounder(t):
if t.minute >= 30:
return t.replace(second=0, microsecond=0, minute=0, hour=t.hour+1)
else:
return t.replace(second=0, microsecond=0, minute=0)
now # 2018-02-22 22:03:53.831589
rounder(now) # 2018-02-22 22:00:00.000000
Here is one way to do it (based on another solution provided here)
def round_to_closest_hour(dttm):
add_hour = True if dttm.minute >= 30 else False
dttm = dttm.replace(second=0, microsecond=0, minute=0)
if add_hour:
dttm += timedelta(hours=1)
return dttm
Basically, check if an hour is needed to be added or not (depending on whether it's past 30 minutes mark). Then reset minutes, seconds, microseconds
and add an hour
if required.
For those who need to change a whole column:
df = df['Timestamp'].dt.round('60min')
You can also use ceil() and floor() functions to replace round, depending on the rules you want to approximate.