pandas - '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 以使其正常工作。
解决方案
推荐阅读
- python - 不明白为什么会出现这个 NameError: name 'null' is not defined 异常
- coldfusion - 调用
不在表单范围内的文件的 MIME 验证算法? - python - 比较对应于 2 个不同日期的值的平均值?
- r - 如何将 Markdown 文件转换为 R DataFrame
- python - 图片高亮效果
- codenameone - 使用 Codename One Preferences 更新“iOS 分发证书”的正确方法
- python - 尝试对两个数据集进行左连接但得到奇怪的结果
- html - 带有父 Div 的粘性侧边栏
- arrays - 使用 ARRAY[] 和 '{}'::int[] 创建数组是否存在性能差异?
- electron - Spectron 可用方法无法正常工作