PacktPublishing / Learn-Algorithmic-Trading

Learn Algorithmic Trading, Published by Packt
MIT License
802 stars 302 forks source link

Error Chapter2\seasonality.py #16

Open MBSCaesar opened 1 year ago

MBSCaesar commented 1 year ago

Chapter2\seasonality.py

Traceback (most recent call last): File "C:\Python310\lib\site-packages\pandas\core\indexes\base.py", line 3803, in get_loc return self._engine.get_loc(casted_key) File "pandas_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc File "pandas_libs\index.pyx", line 165, in pandas._libs.index.IndexEngine.get_loc File "pandas_libs\hashtable_class_helper.pxi", line 2263, in pandas._libs.hashtable.Int64HashTable.get_item File "pandas_libs\hashtable_class_helper.pxi", line 2273, in pandas._libs.hashtable.Int64HashTable.get_item KeyError: 0

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "c:*\Workspace\learn-algorithmic-trading\Chapter2\seasonality.py", line 26, in 'monthly_return': goog_monthly_return[i]}) File "C:\Python310\lib\site-packages\pandas\core\series.py", line 981, in getitem return self._get_value(key) File "C:\Python310\lib\site-packages\pandas\core\series.py", line 1089, in _get_value loc = self.index.get_loc(label) File "C:\Python310\lib\site-packages\pandas\core\indexes\multi.py", line 2916, in get_loc loc = self._get_level_indexer(key, level=0) File "C:\Python310\lib\site-packages\pandas\core\indexes\multi.py", line 3263, in _get_level_indexer idx = self._get_loc_single_level_index(level_index, key) File "C:\Python310\lib\site-packages\pandas\core\indexes\multi.py", line 2849, in _get_loc_single_level_index return level_index.get_loc(key) File "C:\Python310\lib\site-packages\pandas\core\indexes\base.py", line 3805, in get_loc raise KeyError(key) from err KeyError: 0

relentlesss commented 7 months ago

Traceback (most recent call last): File "c:*\Workspace\learn-algorithmic-trading\Chapter2\seasonality.py", line 26, in 'monthly_return': goog_monthly_return[i]}) KeyError: 0

Here you need df.iloc, so replace line 26 with the following: 'monthly_return': goog_monthly_return.iloc[i]})