这需要一些工作,但是请尝试一下。基本思想是找到与每个重采样点最近的两个时间戳并进行插值。
np.searchsorted用于查找最接近重采样点的日期。
# empty frame with desired indexrs = pd.Dataframe(index=df.resample('15min').iloc[1:].index)# array of indexes corresponding with closest timestamp after resampleidx_after = np.searchsorted(df.index.values, rs.index.values)# values and timestamp before/after resamplers['after'] = df.loc[df.index[idx_after], 'Values'].valuesrs['before'] = df.loc[df.index[idx_after - 1], 'Values'].valuesrs['after_time'] = df.index[idx_after]rs['before_time'] = df.index[idx_after - 1]#calculate new weighted valuers['span'] = (rs['after_time'] - rs['before_time'])rs['after_weight'] = (rs['after_time'] - rs.index) / rs['span']# I got errors here unless I turn the index to a seriesrs['before_weight'] = (pd.Series(data=rs.index, index=rs.index) - rs['before_time']) / rs['span']rs['Values'] = rs.eval('before * before_weight + after * after_weight')
毕竟,希望正确的答案是:
In [161]: rs['Values']Out[161]: 1992-08-27 08:00:00 28.0114291992-08-27 08:15:00 28.3139391992-08-27 08:30:00 28.2230301992-08-27 08:45:00 28.9520001992-08-27 09:00:00 29.908571Freq: 15T, Name: Values, dtype: float64
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)