I stumbled over a issue in which I do not understand or can not find a solution in solving.
The codes I type functions perfectly in my Jupyter Notebook but caused an error when I used PyCharm.
I typed in these codes in 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']
Using these codes alone and printing their values are a success. H
However when matched with these lines of code:
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])
it returns this error:
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
Please help, Thank you for your time:)
and not df_ticker.Close[i-1] >= df_ticker [i-2]:
and
KeyError: 5
df_ticker.Close[ 5 ] or df_ticker [ 5 ] doesn't exist.
it was supposed to be:
df_ticker.Close[i] > df_ticker.Close[i-2]
I missed the second Close.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.