使用RedBlackPy 非常容易。它适用于 python 3 的 macosx 和 linux。
import redblackpy as rb
from datetime import datetime
# do not keep Nat values, it is redundantly
# you can interpolate data with no add keys to container
index = [0.782296, 0.821426, 0.864383, 0.906240]
values = [datetime(2012, 11, 19, 12, 40, 10),
datetime(2012, 11, 19, 12, 35, 10),
datetime(2012, 11, 19, 12, 30, 10),
datetime(2012, 11, 19, 12, 25, 10) ]
# init Series with specific interpolation type (floor, ceil, nn, linear)
data = rb.Series(index=index, values=values, dtype='object',
interpolation='linear')
现在您可以使用插值按任意键访问!
# your index, where you wanted to interpolate
int_index = [0.795469, 0.834957]
# access to key that not in series
print( data[int_index[0]] ) # this prints 2012-11-19 12:38:29.005878
# you change interpolation type
data.set_interpolation('floor')
print( data[int_index[0]] ) # this prints 2012-11-19 12:40:10
如果您想向 Series 添加插值,只需使用 insert 或 setitem,如下所示:
# this add interpolation values to data
for el in int_index:
data[el] = data[el]
print(data)
由于最新的插值是'floor' print(data) 的结果:
Series object Untitled
0.782296: 2012-11-19 12:40:10
0.795469: 2012-11-19 12:40:10
0.821426: 2012-11-19 12:35:10
0.834957: 2012-11-19 12:35:10
0.864383: 2012-11-19 12:30:10
0.90624: 2012-11-19 12:25:10