Pandas: AttributeError: 'float' object has no attribute 'MACD'
Pandas: AttributeError: 'float' object has no attribute 'MACD'
我想比较 pandas 数据框中的 2 行,但我总是收到一条错误消息:AttributeError: 'float' object has no attribute 'MACD'.
这是 df:
time open high low close tick_volume spread real_volume EMA_LONG EMA_SHORT MACD SIGNAL HIST 200EMA
0 2018-01-05 03:00:00 1.20775 1.20794 1.20700 1.20724 2887 1 0 1.206134 1.206803 0.000669 0.000669 0.000000 1.207240
1 2018-01-05 04:00:00 1.20723 1.20743 1.20680 1.20710 2349 1 0 1.206216 1.206849 0.000633 0.000649 -0.000016 1.207170
2 2018-01-05 05:00:00 1.20709 1.20755 1.20709 1.20744 1869 1 0 1.206318 1.206941 0.000622 0.000638 -0.000016 1.207261
现在我想根据行中的一些信息计算它会买卖多少次,所以我尝试像这样遍历它:
buy = 0
sell = 0
for i, row in df.iterrows():
if i == 0:
continue
if row.MACD > row.SIGNAL and row[i - 1].MACD < row[i - 1].SIGNAL:
if row.HIST < 0 and row.MACD > row['200EMA'] and row.SIGNAL > row['200EMA']:
buy += 1
elif row.MACD < row.SIGNAL and row[i - 1].MACD > row[i - 1].SIGNAL:
if row.HIST > 0 and row.MACD < row['200EMA'] and row.SIGNAL < row['200EMA']:
sell += 1
print("BUY: " + buy + "SELL: " + sell)
我收到以下错误:
AttributeError: 'float' object has no attribute 'MACD'
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-75-ff4a2b3629bc> in <module>
8 if row.HIST < 0 and row.MACD > row['200EMA'] and row.SIGNAL > row['200EMA']:
9 buy += 1
---> 10 elif row.MACD < row.SIGNAL and row[i - 1].MACD > row[i - 1].SIGNAL:
11 if row.HIST > 0 and row.MACD < row['200EMA'] and row.SIGNAL < row['200EMA']:
12 sell += 1
AttributeError: 'float' object has no attribute 'MACD'
我知道这里已经出现了这个错误,但是那里的解决方案对我没有帮助。
已经谢谢你了!
你的问题在他这里行[i - 1].MACD
当您访问第 [i-1] 行时,您将获得该位置在服务中的值
如果 i = 1 那么你将获得该行的行 [0] 而不是数据框中的 preivice 行你应该通过 df.iloc[i-1].MACD
切换它
我想比较 pandas 数据框中的 2 行,但我总是收到一条错误消息:AttributeError: 'float' object has no attribute 'MACD'.
这是 df:
time open high low close tick_volume spread real_volume EMA_LONG EMA_SHORT MACD SIGNAL HIST 200EMA
0 2018-01-05 03:00:00 1.20775 1.20794 1.20700 1.20724 2887 1 0 1.206134 1.206803 0.000669 0.000669 0.000000 1.207240
1 2018-01-05 04:00:00 1.20723 1.20743 1.20680 1.20710 2349 1 0 1.206216 1.206849 0.000633 0.000649 -0.000016 1.207170
2 2018-01-05 05:00:00 1.20709 1.20755 1.20709 1.20744 1869 1 0 1.206318 1.206941 0.000622 0.000638 -0.000016 1.207261
现在我想根据行中的一些信息计算它会买卖多少次,所以我尝试像这样遍历它:
buy = 0
sell = 0
for i, row in df.iterrows():
if i == 0:
continue
if row.MACD > row.SIGNAL and row[i - 1].MACD < row[i - 1].SIGNAL:
if row.HIST < 0 and row.MACD > row['200EMA'] and row.SIGNAL > row['200EMA']:
buy += 1
elif row.MACD < row.SIGNAL and row[i - 1].MACD > row[i - 1].SIGNAL:
if row.HIST > 0 and row.MACD < row['200EMA'] and row.SIGNAL < row['200EMA']:
sell += 1
print("BUY: " + buy + "SELL: " + sell)
我收到以下错误:
AttributeError: 'float' object has no attribute 'MACD'
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-75-ff4a2b3629bc> in <module>
8 if row.HIST < 0 and row.MACD > row['200EMA'] and row.SIGNAL > row['200EMA']:
9 buy += 1
---> 10 elif row.MACD < row.SIGNAL and row[i - 1].MACD > row[i - 1].SIGNAL:
11 if row.HIST > 0 and row.MACD < row['200EMA'] and row.SIGNAL < row['200EMA']:
12 sell += 1
AttributeError: 'float' object has no attribute 'MACD'
我知道这里已经出现了这个错误,但是那里的解决方案对我没有帮助。
已经谢谢你了!
你的问题在他这里行[i - 1].MACD
当您访问第 [i-1] 行时,您将获得该位置在服务中的值 如果 i = 1 那么你将获得该行的行 [0] 而不是数据框中的 preivice 行你应该通过 df.iloc[i-1].MACD
切换它