I want to be able to round an entire date column to whatever two 12hr times the values are closest to.
For example, if I want the column to be rounded to either 8am or 8pm.
dates = pd.to_datetime(['2022-01-28 15:25:22.456', '2022-01-27'])
Should become this
dates2 = pd.to_datetime(['2022-01-28 20:00:00', '2022-01-27 08:00:00'])
DatetimeIndex(['2022-01-28 20:00:00', '2022-01-27 08:00:00'], dtype='datetime64[ns]', freq=None)
This was the solution I came up with thanks to other answers.
Here's a workable example.
from datetime import timedelta
step = timedelta(minutes=30)
start = datetime(2022,1,1,0,0,0)
end = datetime(2022,1,2,0,0,0)
seconds = (end - start).total_seconds()
array = []
for i in range(0, int(seconds), int(step.total_seconds())):
array.append(start + timedelta(seconds=i))
dates = pd.Series(pd.to_datetime(array))
Everything before 8am gets rounded up to 8am, everything after is rounded up to 8pm. Anything after 8pm is rounded to 8am the next day.
dates2 = (dates-pd.Timedelta(hours=8)).dt.ceil('12H')+pd.Timedelta(hours=8)
pd.concat([dates,dates2],axis=1)