Josh Friedlander
Josh Friedlander

Reputation: 11657

Rounding df index along with its data to nearest interval

I have a time series dataframe with a DateTimeIndex, based on sensor data which sometimes arrives a bit early or a bit late. It looks something like this:

df = pd.DataFrame(np.ones(3), index=pd.DatetimeIndex([
                 '2021-01-01 08:00', '2021-01-01 08:04', '2021-01-01 08:11']))
> df
2021-01-01 08:00:00     1.0
2021-01-01 08:04:00     1.0
2021-01-01 08:11:00     1.0

I'd like to rearrange it to match five-minute intervals without losing any data. I tried:

df.reindex(df.index.round('5 min'))

but it drops the data not matching the intervals:

2021-01-01 08:00:00     1.0
2021-01-01 08:05:00     NaN
2021-01-01 08:10:00     NaN

Is there a way to get this?

2021-01-01 08:00:00     1.0
2021-01-01 08:05:00     1.0
2021-01-01 08:10:00     1.0

Upvotes: 2

Views: 31

Answers (1)

jezrael
jezrael

Reputation: 862481

I think you need method='nearest' in DataFrame.reindex:

df = df.reindex(df.index.round('5 min'), method='nearest')
print (df)
    
                       0
2021-01-01 08:00:00  1.0
2021-01-01 08:05:00  1.0
2021-01-01 08:10:00  1.0

Upvotes: 2

Related Questions