首页 > 解决方案 > 'DataFrame' 对象是可变的,因此它们不能被散列

问题描述

我正在尝试运行以下代码

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 以使其正常工作。

标签: pandasfunctiondataframe

解决方案


推荐阅读