调整 heikin ashi 代码错误:列表喜欢到 .loc 或 [] 与任何缺少的标签不再支持
Posted
技术标签:
【中文标题】调整 heikin ashi 代码错误:列表喜欢到 .loc 或 [] 与任何缺少的标签不再支持【英文标题】:Adapting heikin ashi code ERROR: list-likes to .loc or [] with any missing labels is no longer supported 【发布时间】:2021-04-21 00:35:21 【问题描述】:我正在尝试修改我在网上找到的一些用于 heikin ashi 蜡烛的代码
代码是:
def heikin_ashi(self, df):
heikin_ashi_df = pd.DataFrame(index=df.index.values, columns=['HE_Open', 'HE_High', 'HE_Low', 'HE_Close'])
heikin_ashi_df['HE_Close'] = (df['Open'] + df['High'] + df['Low'] + df['Close']) / 4
for i in range(len(df)):
if i == 0:
heikin_ashi_df.iat[0, 0] = df['Open'].iloc[0]
else:
heikin_ashi_df.iat[i, 0] = (heikin_ashi_df.iat[i - 1, 0] + heikin_ashi_df.iat[i - 1, 3]) / 2
heikin_ashi_df['HE_High'] = heikin_ashi_df.loc[:, ['HE_Open', 'HE_Close']].join(df['High']).max(axis=1)
heikin_ashi_df['HE_Low'] = heikin_ashi_df.loc[:, ['HE_Open', 'HE_Close']].join(df['Low']).min(axis=1)
return heikin_ashi_df
但是这个方法返回一个新的 pandas 表,我只想将新行添加到我现有的表中
这是我的尝试:
def heikin_ashi_full(self, df):
df.append(pd.Series(name='HE_Open', dtype=float))
df.append(pd.Series(name='HE_High', dtype=float))
df.append(pd.Series(name='HE_Low', dtype=float))
df.append(pd.Series(name='HE_Close', dtype=float))
df['HE_Close'] = (df['Open'] + df['High'] + df['Low'] + df['Close']) / 4
for i in range(len(df)):
if i == 0:
df.iat[0, 0] = df['Open'].iloc[0]
else:
df.iat[i, 0] = (df.iat[i - 1, 0] + df.iat[i - 1, 3]) / 2
df['HE_High'] = df.loc[:, ['HE_Open', 'HE_Close', 'High']].max(axis=1)
df['HE_Low'] = df.loc[:, ['HE_Open', 'HE_Close', 'Low']].min(axis=1)
return df
不幸的是,该方法在该方法的第 15 行崩溃
df['HE_High'] = df.loc[:, ['HE_Open', 'HE_Close', 'High']].max(axis=1)
回溯显示在这里:
主表是从具有如下数据的文件中创建的:
Date,Open,High,Low,Close,Volume,OpenInterest
2020-08-25 06:00,15.12,15.25,14.82,15.05,3824776,0.0
2020-08-26 06:00,15.09,15.44,15.02,15.07,5933882,0.0
2020-08-27 06:00,15.22,15.33,14.672,14.84,5728962,0.0
2020-08-28 06:00,15.01,15.18,14.8605,15.12,5992532,0.0
2020-08-31 06:00,15.23,15.23,14.62,14.62,7000994,0.0
2020-09-01 06:00,14.64,15.18,14.24,15.08,7347598,0.0
有趣的是,我从互联网上获得的第一个方法似乎工作得很好而且很准确。 我不熟悉熊猫,不知道我是怎么做错的,也找不到解决办法。 非常感谢您的帮助,并提前感谢您的宝贵时间。
【问题讨论】:
1) 您能否链接到描述如何计算它的 Heikin Ashi 方法的描述?我找到的资源有点模糊。 2) 我认为您可以通过shifting one of your columns by one 避免解决方案中的循环。TraceBack
应该以文本形式发布。 No Screenshots 的代码或数据。始终为 minimal reproducible example 提供代码、数据、错误、当前输出和预期输出,如 formatted text。如果相关,绘图图像是可以的。请参阅How to ask a good question。
【参考方案1】:
我尝试了您的代码,我的第一个问题是我正在尝试使用 df.append()
添加列,但我认为这是因为未添加列。如果我在将开盘价、最高价和最低价添加为空后尝试运行您的代码,将显示结果。数据框的列结构发生了一些变化,因此我对其进行了相应的修改。另外,我认为this answer 关于 SO 上的“heikin-ashi”会有所帮助。
import pandas as pd
import numpy as np
import io
data = '''
Date,Open,High,Low,Close,Volume,OpenInterest
2020-08-25 06:00,15.12,15.25,14.82,15.05,3824776,0.0
2020-08-26 06:00,15.09,15.44,15.02,15.07,5933882,0.0
2020-08-27 06:00,15.22,15.33,14.672,14.84,5728962,0.0
2020-08-28 06:00,15.01,15.18,14.8605,15.12,5992532,0.0
2020-08-31 06:00,15.23,15.23,14.62,14.62,7000994,0.0
2020-09-01 06:00,14.64,15.18,14.24,15.08,7347598,0.0
'''
df = pd.read_csv(io.StringIO(data), sep=',')
o = pd.Series([], index=[], name='HE_Open', dtype=float)
h = pd.Series([], index=[], name='HE_High', dtype=float)
l = pd.Series([], index=[], name='HE_Low', dtype=float)
df = pd.concat([df, o, h, l],axis=1)
df['HE_Close'] = (df['Open'] + df['High'] + df['Low'] + df['Close']) / 4
for i in range(len(df)):
if i == 0:
df.iat[0, 7] = df['Open'].iloc[0]
else:
df.iat[i, 7] = (df.iat[i - 1, 1] + df.iat[i - 1, 4]) / 2
df['HE_High'] = df.loc[:, ['HE_Open', 'HE_Close', 'High']].max(axis=1)
df['HE_Low'] = df.loc[:, ['HE_Open', 'HE_Close', 'Low']].min(axis=1)
df
Date Open High Low Close Volume OpenInterest HE_Open HE_High HE_Low HE_Close
0 2020-08-25 06:00 15.12 15.25 14.8200 15.05 3824776 0.0 15.120 15.25 14.8200 15.060000
1 2020-08-26 06:00 15.09 15.44 15.0200 15.07 5933882 0.0 15.085 15.44 15.0200 15.155000
2 2020-08-27 06:00 15.22 15.33 14.6720 14.84 5728962 0.0 15.080 15.33 14.6720 15.015500
3 2020-08-28 06:00 15.01 15.18 14.8605 15.12 5992532 0.0 15.030 15.18 14.8605 15.042625
4 2020-08-31 06:00 15.23 15.23 14.6200 14.62 7000994 0.0 15.065 15.23 14.6200 14.925000
5 2020-09-01 06:00 14.64 15.18 14.2400 15.08 7347598 0.0 14.925 15.18 14.2400 14.785000
【讨论】:
以上是关于调整 heikin ashi 代码错误:列表喜欢到 .loc 或 [] 与任何缺少的标签不再支持的主要内容,如果未能解决你的问题,请参考以下文章
将 headerView 添加到小部件上的小部件中的 listView 调整大小 android