Pandas Extrema 但仅与左侧相比

问题描述 投票:0回答:0
def is_edge_pivot_low(df, src, i, n):
    if i < n:
        # n values before index don't exist
        return False
    # True if n values are higher than value at current index
    return df[src].iloc[i-n:i+1].idxmin() == df.index[i]

def find_edge_lows(df, src, n):
    results = []
    for i in range(len(df)):
        if is_edge_pivot_low(df, src, i, n):
            results.append(df[src][i])
        else:
            results.append(np.nan)
    return results

# Example
df['EdgePivotLow'] = find_edge_lows(df, 'SomeColumn', 5)

此代码将找到较低的枢轴,但只比较左侧的 n 个值。我知道这是非常低效的,但我找不到正确的解决方案来用 scipy 左右正确地做到这一点......也许有人知道如何做得更好。

pandas scipy pivot maxima minima
© www.soinside.com 2019 - 2024. All rights reserved.