我有定期数据,索引是浮点数,如下所示:
time = [0, 0.1, 0.21, 0.31, 0.40, 0.49, 0.51, 0.6, 0.71, 0.82, 0.93]
voltage = [1, -1, 1.1, -0.9, 1, -1, 0.9,-1.2, 0.95, -1.1, 1.11]
df = DataFrame(data=voltage, index=time, columns=['voltage'])
df.plot(marker='o')
Run Code Online (Sandbox Code Playgroud)
我想创建一个cross(df, y_val, direction='rise' | 'fall' | 'cross')函数,返回一个时间数组(索引),其中包含所有插值点,其中电压值等于y_val.对于"上升",仅返回斜率为正的值; 对于'跌倒',只有具有负斜率的值才会被撤销; 对于'交叉'两者都返回.因此,如果y_val = 0且direction ='cross',那么将返回具有10个值的数组,其中交叉点的X值(第一个约为0.025).
我在想这可以用迭代器完成,但想知道是否有更好的方法来做到这一点.
谢谢.我喜欢熊猫和熊猫社区.
dai*_*len 16
为此,我最终得到了以下内容.它是一个矢量化版本,比使用循环的版本快150倍.
def cross(series, cross=0, direction='cross'):
"""
Given a Series returns all the index values where the data values equal
the 'cross' value.
Direction can be 'rising' (for rising edge), 'falling' (for only falling
edge), or 'cross' for both edges
"""
# Find if values are above or bellow yvalue crossing:
above=series.values > cross
below=np.logical_not(above)
left_shifted_above = above[1:]
left_shifted_below = below[1:]
x_crossings = []
# Find indexes on left side of crossing point
if direction == 'rising':
idxs = (left_shifted_above & below[0:-1]).nonzero()[0]
elif direction == 'falling':
idxs = (left_shifted_below & above[0:-1]).nonzero()[0]
else:
rising = left_shifted_above & below[0:-1]
falling = left_shifted_below & above[0:-1]
idxs = (rising | falling).nonzero()[0]
# Calculate x crossings with interpolation using formula for a line:
x1 = series.index.values[idxs]
x2 = series.index.values[idxs+1]
y1 = series.values[idxs]
y2 = series.values[idxs+1]
x_crossings = (cross-y1)*(x2-x1)/(y2-y1) + x1
return x_crossings
# Test it out:
time = [0, 0.1, 0.21, 0.31, 0.40, 0.49, 0.51, 0.6, 0.71, 0.82, 0.93]
voltage = [1, -1, 1.1, -0.9, 1, -1, 0.9,-1.2, 0.95, -1.1, 1.11]
df = DataFrame(data=voltage, index=time, columns=['voltage'])
x_crossings = cross(df['voltage'])
y_crossings = np.zeros(x_crossings.shape)
plt.plot(time, voltage, '-ob', x_crossings, y_crossings, 'or')
plt.grid(True)
Run Code Online (Sandbox Code Playgroud)
当这个工作时,它非常令人满意.可以做出哪些改进?