MyTT icon indicating copy to clipboard operation
MyTT copied to clipboard

希望ref函数中参数n支持序列值

Open hotwind2015 opened this issue 2 years ago • 5 comments

hotwind2015 avatar Dec 01 '23 05:12 hotwind2015

I'm facing the same issue. Please add this support.

kevinyang72 avatar Jan 28 '24 07:01 kevinyang72

I'm facing the same issue. Please add this support. 试试下面的这个

def REF(S, N=1):  # 对序列整体下移动N,返回序列(shift后会产生NAN)
    if isinstance(N, (int, float)):
        return pd.Series(S).shift(N).values
    else:
        res = np.repeat(np.nan, len(S))
        for i in range(len(S)):
            if (not np.isnan(N[i])):
                res[i] = S[N[i]]
        return res

hotwind2015 avatar Jan 29 '24 09:01 hotwind2015

Does not seem working for me. How the shift works here with "res[i] = S[N[i]]"?

kevinyang72 avatar Jan 30 '24 07:01 kevinyang72

Does not seem working for me. How the shift works here with "res[i] = S[N[i]]"?

try this

def REF(S, N=1):  # 对序列整体下移动N,返回序列(shift后会产生NAN)
    if isinstance(N, (int, float)):
        return pd.Series(S).shift(N).values
    else:
        res = np.repeat(np.nan, len(S))
        for i in range(len(S)):
            if (not np.isnan(N[i])):
                res[i] = pd.Series(S).shift(N[i])[i]
        return res

S=[0,1,2,3,4,5]
N = [1,2,2,3,3,1]
res = REF(S,N)
print(res)

# will print [nan nan  0.  0.  1.  4.]

hotwind2015 avatar Jan 30 '24 12:01 hotwind2015

Excellent! It works!

kevinyang72 avatar Jan 30 '24 16:01 kevinyang72