我正在尝试运行以下代码
def RSI(series, period):
delta = series.diff().dropna()
u = delta * 0
d = u.copy()
u[delta > 0] = delta[delta > 0]
d[delta < 0] = -delta[delta < 0]
u[u.index[period-1]] = np.mean( u[:period] ) #first value is sum of avg gains
u = u.drop(u.index[:(period-1)])
d[d.index[period-1]] = np.mean( d[:period] ) #first value is sum of avg losses
d = d.drop(d.index[:(period-1)])
rs = u.ewm(com=period-1, min_periods = period).mean() / \
d.ewm(com=period-1, min_periods = period).mean()
return 100 - 100 / (1 + rs)
df['RSI'] = df.groupby(['Commodity'], as_index = False).apply(RSI(df, 14)).reset_index(level=0, drop = True)
我不断收到以下错误:“'DataFrame'对象是可变的,因此无法进行哈希处理”。
但是,当我使用以下命令时,它会起作用:
df['RSI'] = RSI(df['Close'], 14)
我在这里想念什么?我需要插入一个Groupby才能使其正常工作。