MyTT
MyTT copied to clipboard
希望ref函数中参数n支持序列值
I'm facing the same issue. Please add this support.
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
Does not seem working for me. How the shift works here with "res[i] = S[N[i]]"?
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.]
Excellent! It works!