我目前正在尝试清理并使用pandas填写一些丢失的时间序列数据。插值函数运行良好,但它没有一些(不太广泛使用)插值函数,我需要我的数据集。几个例子就是一个简单的" last"有效的数据点可以创建类似于阶梯函数的东西,或类似对数或几何插值的东西。
浏览文档时,似乎没有一种方法可以传递自定义插值函数。这些功能是否直接存在于熊猫中?如果没有,有没有人做过任何pandas-fu通过其他方式有效地应用自定义插值?
答案 0 :(得分:3)
Pandas提供的插值方法是scipy.interpolate.interp1d
提供的插值方法 - 遗憾的是,它们似乎无法以任何方式扩展。我不得不做类似的事情来应用SLERP四元数插值(使用numpy-quaternion),我设法做得非常有效。我会在这里复制代码,希望您可以根据自己的需要进行调整:
def interpolate_slerp(data):
if data.shape[1] != 4:
raise ValueError('Need exactly 4 values for SLERP')
vals = data.values.copy()
# quaternions has size Nx1 (each quaternion is a scalar value)
quaternions = quaternion.as_quat_array(vals)
# This is a mask of the elements that are NaN
empty = np.any(np.isnan(vals), axis=1)
# These are the positions of the valid values
valid_loc = np.argwhere(~empty).squeeze(axis=-1)
# These are the indices (e.g. time) of the valid values
valid_index = data.index[valid_loc].values
# These are the valid values
valid_quaternions = quaternions[valid_loc]
# Positions of the missing values
empty_loc = np.argwhere(empty).squeeze(axis=-1)
# Missing values before first or after last valid are discarded
empty_loc = empty_loc[(empty_loc > valid_loc.min()) & (empty_loc < valid_loc.max())]
# Index value for missing values
empty_index = data.index[empty_loc].values
# Important bit! This tells you the which valid values must be used as interpolation ends for each missing value
interp_loc_end = np.searchsorted(valid_loc, empty_loc)
interp_loc_start = interp_loc_end - 1
# These are the actual values of the interpolation ends
interp_q_start = valid_quaternions[interp_loc_start]
interp_q_end = valid_quaternions[interp_loc_end]
# And these are the indices (e.g. time) of the interpolation ends
interp_t_start = valid_index[interp_loc_start]
interp_t_end = valid_index[interp_loc_end]
# This performs the actual interpolation
# For each missing value, you have:
# * Initial interpolation value
# * Final interpolation value
# * Initial interpolation index
# * Final interpolation index
# * Missing value index
interpolated = quaternion.slerp(interp_q_start, interp_q_end, interp_t_start, interp_t_end, empty_index)
# This puts the interpolated values into place
data = data.copy()
data.iloc[empty_loc] = quaternion.as_float_array(interpolated)
return data
诀窍在np.searchsorted
,它可以很快找到每个值的正确插值结束。这种方法的局限性在于:
答案 1 :(得分:2)
为了找到Series
内的缺失数据块,您可以按照Finding consecutive segments in a pandas data frame的方式执行操作:
s = pd.Series([1, 2, np.nan, np.nan, 5, 6, np.nan, np.nan, np.nan, 10])
x = s.isnull().reset_index(name='null')
# computes unique numbers for each block of consecutive nan/non-nan values
x['block'] = (x['null'].shift(1) != x['null']).astype(int).cumsum()
# select those blocks that relate to null values
x[x['null']].groupby('block')['index'].apply(np.array)
这将产生以下系列,其中值是包含每个块的nan值的所有索引条目的数组:
block
2 [2, 3]
4 [6, 7, 8]
Name: index, dtype: object
您可以迭代这些并应用自定义修复逻辑。之前和之后获得价值应该很容易。