![](/img/trans.png)
[英]I have an error when importing modules using PyCharm that does not occur when using Jupyter Notebook
[英]Key error 5 issue when using Jupyter notebook commands into pycharm
我偶然發現了一個我不理解或找不到解決方案的問題。
我鍵入的代碼在我的 Jupyter Notebook 中運行完美,但在我使用 PyCharm 時出現錯誤。
我在 PyCharm 中輸入了這些代碼:
df_ticker.loc[df_ticker['Red'] == True, 'Value'] = df_ticker['Open'] - df_ticker['Close']
df_ticker.loc[df_ticker['Red'] == False, 'Value'] = df_ticker['Close'] - df_ticker['Open']
單獨使用這些代碼並打印它們的值是成功的。 H
但是,當與這些代碼行匹配時:
for i in range(1, len(df_ticker)):
if df_ticker.Close[i] > df_ticker.Close[i-1] and df_ticker.Red[i] == False and \
df_ticker.Value.round(decimals = 0)[i] >= df_ticker.Value.round(decimals = 0)[i-1] \
and not df_ticker.Close[i-1] >= df_ticker [i-2]:
rise.append(df_ticker.Close[i])
它返回此錯誤:
Traceback (most recent call last):
File "/Users/benjaminwong/PycharmProjects/Yahoo Finance project/venv/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 2889, in get_loc
return self._engine.get_loc(casted_key)
File "pandas/_libs/index.pyx", line 70, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 97, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1675, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1683, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 5
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/benjaminwong/PycharmProjects/Yahoo Finance project/Rise_Finder 2.py", line 29, in <module>
and not df_ticker.Close[i-1] >= df_ticker [i-2]:
File "/Users/benjaminwong/PycharmProjects/Yahoo Finance project/venv/lib/python3.8/site-packages/pandas/core/frame.py", line 2899, in __getitem__
indexer = self.columns.get_loc(key)
File "/Users/benjaminwong/PycharmProjects/Yahoo Finance project/venv/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 2891, in get_loc
raise KeyError(key) from err
KeyError: 5
請幫忙,謝謝你的時間:)
and not df_ticker.Close[i-1] >= df_ticker [i-2]:
和
KeyError: 5
df_ticker.Close[ 5 ] 或 df_ticker [ 5 ] 不存在。
它應該是:
df_ticker.Close[i] > df_ticker.Close[i-2]
我錯過了第二次關閉。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.