【问题标题】:Money Flow Index keyerror资金流量指数关键错误
【发布时间】:2020-10-24 16:57:16
【问题描述】:

我已经从示例股票(在本例中为 Apple)获得了历史值,并且正在按照我在网上看到的示例进行操作,但是,当他们的代码成功时,我的代码因一些关键错误而失败? 谁能告诉/告诉我出了什么问题,并希望如何解决?错误是:

KeyError                                  Traceback (most recent call last)
~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2645             try:
-> 2646                 return self._engine.get_loc(key)
   2647             except KeyError:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 1

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-60-d89407b24e87> in <module>
      3 
      4 for i in range(1, len(typical_price)):
----> 5     if typical_price[i] > typical_price[i-1]:
      6         positive_flow.append(money_flow[i-1])
      7         negative_flow.append(0)

~\anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2798             if self.columns.nlevels > 1:
   2799                 return self._getitem_multilevel(key)
-> 2800             indexer = self.columns.get_loc(key)
   2801             if is_integer(indexer):
   2802                 indexer = [indexer]

~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2646                 return self._engine.get_loc(key)
   2647             except KeyError:
-> 2648                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2649         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2650         if indexer.ndim > 1 or indexer.size > 1:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 1

代码是:

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import math
import pandas_datareader as pdr

stocks = ['AAPL']
data_close = pdr.get_data_yahoo(stocks, start='2020-01-01')['Close']
data_high = pdr.get_data_yahoo(stocks, start='2020-01-01')['High']
data_low = pdr.get_data_yahoo(stocks, start='2020-01-01')['Low']
data_volume = pdr.get_data_yahoo(stocks, start='2020-01-01')['Volume']
typical_price = (data_close + data_high + data_low)/3;
money_flow = typical_price * data_volume;
    
positive_flow = []
negative_flow = []

for i in range(1, len(typical_price)):
    if typical_price[i] > typical_price[i-1]:
        positive_flow.append(money_flow[i-1])
        negative_flow.append(0)
    elif typical_price[i] < typical_price[i-1]:
        positive_flow.append(0)
        negative_flow.append(money_flow[i-1])
    else:
        positive_flow.append(0)
        negative_flow.append(0)

当我运行代码的最后部分时出现错误,我尝试为我的 MFI 算法检索正负资金流。

【问题讨论】:

    标签: python pandas dataframe jupyter-notebook


    【解决方案1】:

    使用iloc 索引

    for i in range(1, len(typical_price)):
        if typical_price.iloc[i].item() > typical_price.iloc[i-1].item():
            positive_flow.append(money_flow.iloc[i-1])
            negative_flow.append(0)
        elif typical_price.iloc[i].item() < typical_price.iloc[i-1].item():
            positive_flow.append(0)
            negative_flow.append(money_flow.iloc[i-1])
        else:
            positive_flow.append(0)
            negative_flow.append(0)
    
    

    typical_pricemoney_flow 可能将日期时间作为索引而不是整数。如果你想通过整数位置访问行,那么你可以使用iloc

    【讨论】:

    • 这是做什么的?会试试的!
    • @RobinSvensson 它将根据整数位置给出行。
    • 我现在收到以下错误:ValueError ValueError:一个系列的真值是不明确的。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。
    • 是的,抱歉我没有测试过。 iloc 将有返回系列,因此您需要使用 typical_price.iloc[i].item()typical_price.iloc[i].to_numpy() 才能从中获取价值。
    猜你喜欢
    • 2018-08-26
    • 2017-08-15
    • 2018-05-03
    • 2023-02-03
    • 1970-01-01
    • 2018-03-20
    • 2016-02-27
    • 2023-02-19
    • 2021-06-09
    相关资源
    最近更新 更多