pandas Python Numpy pct_change in reverse

f45qwnt8  于 2023-08-01  发布在  Python
关注(0)|答案(1)|浏览(91)

我使用numpy来获取一段时间内数据点之间的变化。

df["ema_10_diff"] = df["mid_price"].pct_change(10 * 60)

字符串
现在我需要做的是把它还原成随时间的累积变化。如果期间设置为标准期间= 1。然后我会写这个:

df["ema_10_diff_reconstructed"] = (df["ema_10_diff"] + 1).cumprod()


但是因为周期是10 * 60,所以我不能这样做,并且cumprod没有周期参数。有没有人知道如何重建原始价值观。我知道它将从1开始,而不是真正的原始值,但它将显示变化的累积百分比。

zpjtge22

zpjtge221#

我想出来了!我需要在我的百分比更改列表前加上原始值的期间量,以回到原始值。

# Get percent difference
df["ema_10_diff"] = df["mid_price"].pct_change(10 * 60) + 1

# Prepend data with original values of length: period
x = (
        df["mid_price"][: 10 * 60]
        .append(df["ema_10_diff"], ignore_index=True)
    )

# Function to get the cumulative product with period in between.
def cumprod_period(series: pd.Series, period=1) -> pd.Series:
    output = list(series[:period])
    series_length = len(series)
    for i in range(period, series_length):
        current_item = series[i]
        previous_item = output[-period]
        prod = current_item * previous_item

        output.append(prod)

    return pd.Series(output)

# Result is same as df['mid_price']
df["reconstructed_mid_price"] = cumprod_period(x, 10 * 60)

字符串
下面是我写的单元测试,以检查它是否有效:

def test_cumprod_period():
    """Tests if the function works"""
    input = [10, 20, 80, 20, 20, 10]

    period = 2

    # Prepended the first 2 values of the original list to the percentage 
    changed.
    pct_change = pd.Series([10, 20, 8.0, 1.0, 0.25, 0.5])

    output = cumprod_period(pct_change, period)

    assert list(output) == input, f"{list(output)} != {input}"

相关问题