I encountered a strange, very unexpected behavior in the round-method of pandas.DatetimeIndex:
import pandas as pd
import datetime as dt
t1 = pd.DatetimeIndex([dt.datetime(2013,12,5,1,30,0),
dt.datetime(2013,12,5,2,30,0),
dt.datetime(2013,12,5,3,30,0),
dt.datetime(2013,12,5,4,30,0)])
print(t1)
gives:
DatetimeIndex(['2013-12-05 01:30:00', '2013-12-05 02:30:00',
'2013-12-05 03:30:00', '2013-12-05 04:30:00'],
dtype='datetime64[ns]', freq=None)
So far, so good. Now I want to round to the nearest full hour. I don't mind if the next or the previous hour is chosen. But I need consistent behavior.
t2 = t1.round('H')
print(t2)
Surprisingly I get:
DatetimeIndex(['2013-12-05 02:00:00', '2013-12-05 02:00:00',
'2013-12-05 04:00:00', '2013-12-05 04:00:00'],
dtype='datetime64[ns]', freq=None)
Entries 1 and 3 got rounded up while entries 2 and 4 got rounded down. Is this supposed behavior? I guess there is some numerical stuff going on under the hood. But this is really disturbing. In my case the temporal resolution is constrained to minutes. So I can add (or subtract) 1s to every time and get the desired result. But this can't be the right way to do it.