首页 > 解决方案 > 如何修复 pandas datafrom 中的此错误 - KeyError: 'Zone% (pfx)'

问题描述

我正在尝试在 pybaseball 包上做一个简单的数据框。这一切都有效,直到本周 28/3/2021。但现在我收到以下错误:

KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2897             try:
-> 2898                 return self._engine.get_loc(casted_key)
   2899             except KeyError as err:

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: 'Zone% (pfx)'

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

KeyError                                  Traceback (most recent call last)
4 frames
/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2898                 return self._engine.get_loc(casted_key)
   2899             except KeyError as err:
-> 2900                 raise KeyError(key) from err
   2901 
   2902         if tolerance is not None:

KeyError: 'Zone% (pfx)'

这是我运行的代码:

import pandas as pd

pd.set_option('display.max_rows', None)

pd.set_option('display.max_columns', None)

pd.set_option('chained_assignment', None)

%pip install pybaseball==2.0.0

import pybaseball as pyb

df_2020 = pyb.batting_stats(2020)

这是在 google colab 中运行的。

任何帮助表示赞赏

标签: pandaskeyerror

解决方案


不确定您是否找到了解决方案,但我看到他们发布了一个新的 pybaseball,版本 2.2.0,似乎不再抛出错误


推荐阅读