Панды, предоставляющие KeyError: 0 после создания новой среды с Python 3.6 - PullRequest
2 голосов
/ 15 мая 2019
df = pd.read_html('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')[0][0][1:]

выдаёт мне следующую ошибку

Traceback (most recent call last):
  File "/Users/shijith/anaconda3/envs/py36/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 2657, in get_loc
    return self._engine.get_loc(key)
  File "pandas/_libs/index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1601, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1608, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 0

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "delete.py", line 76, in <module>
    stock_Symbols = pd.read_html('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')[0][0]
  File "/Users/shijith/anaconda3/envs/py36/lib/python3.6/site-packages/pandas/core/frame.py", line 2927, in __getitem__
    indexer = self.columns.get_loc(key)
  File "/Users/shijith/anaconda3/envs/py36/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 2659, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas/_libs/index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1601, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1608, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 0

df = pd.read_html('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')[0] работает правильно, при попытке повторного среза возникает ошибка.

Я использую anaconda и у меня Python 3.7установлен на моей базовой среде.Но так как мне пришлось использовать H20, я создал новую среду с Python 3.6.

В базовой версии приведенный выше код работал без ошибок.Может кто-нибудь объяснить, почему это происходит?

1 Ответ

0 голосов
/ 15 мая 2019

Может быть потому, что я использовал source activate py36env для активации созданной среды и установил в нее пакеты (я использую conda 4.6.14).

В соответствии с документами conda для активации созданной средыиспользуйте:

Для conda 4.6 и более поздних версий используйте.
conda activate и conda deactivate

Для версий conda до 4.6 , используйте:
Windows: activate и deactivate
Linux и macOS: source activate и source deactivate

Мне пришлось удалить эту среду и создать заново (conda create -n envname python=3.5 pandas scikit-learn), активируется с помощью conda activate envname

, теперь работает

...