KeyError:python3.7 / site-packages / pandas / core / indexes / base.py中的“ class_name”

时间:2019-11-29 22:03:38

标签: python pandas

我正在尝试使用一个Github存储库,并且在python源文件中出现以下错误。 我看过[this] [1]之类的帖子,但找不到确切的问题。

这是我看到的错误:

File "/home/kgarg8/kgarg8-workspace/few-shot/venv/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 3078, in get_loc
return self._engine.get_loc(key)                             
File "pandas/_libs/index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'class_name'                                     

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main "__main__", mod_spec)
File "/usr/lib/python3.7/runpy.py", line 85, in _run_code exec(code, run_globals)
File "/home/kgarg8/kgarg8-workspace/few-shot/experiments/proto_nets.py", line 62, in <module> background = dataset_class('background')
File "/home/kgarg8/kgarg8-workspace/few-shot/few_shot/datasets.py", line 31, in __init__
self.unique_characters = sorted(self.df['class_name'].unique())

File "/home/kgarg8/kgarg8-workspace/few-shot/venv/lib/python3.7/site-packages/pandas/core/frame.py", line 2688, in __getitem__               
  return self._getitem_column(key)
File "/home/kgarg8/kgarg8-workspace/few-shot/venv/lib/python3.7/site-packages/pandas/core/frame.py", line 2695, in _getitem_column           
  return self._get_item_cache(key)                                                                                                           
File "/home/kgarg8/kgarg8-workspace/few-shot/venv/lib/python3.7/site-packages/pandas/core/generic.py", line 2489, in _get_item_cache         
  values = self._data.get(item)                                                                                                              
File "/home/kgarg8/kgarg8-workspace/few-shot/venv/lib/python3.7/site-packages/pandas/core/internals.py", line 4115, in get                   
  loc = self.items.get_loc(item)

File "/home/kgarg8/kgarg8-workspace/few-shot/venv/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 3080, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer key))    
File "pandas/_libs/index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'class_name'

以下是相关代码段:

# proto_nets.py
if args.dataset == 'omniglot':
    n_epochs = 40
    dataset_class = OmniglotDataset
    num_input_channels = 1
    drop_lr_every = 20
...
background = dataset_class('background')

# datasets.py
class OmniglotDataset(Dataset):
    def __init__(self, subset):
        if subset not in ('background', 'evaluation'):
            raise(ValueError, 'subset must be one of (background, evaluation)')
        self.subset = subset
        self.df = pd.DataFrame(self.index_subset(self.subset))
        self.df = self.df.assign(id=self.df.index.values)
        self.unique_characters = sorted(self.df['class_name'].unique())

您可以假设我是新手,希望进一步调试的任何指针。 我认为问题是由于Python / Pandas版本问题所致。 我在pandas == 0.23.4和python == 3.7.3上运行

1 个答案:

答案 0 :(得分:1)

该错误是由于您处理唯一值(self.unique_characters)的方式所致,尤其是在df['class_name']处。该块正在寻找名为class_name的列,并且您显然没有这样的列。相反,我相信您可以实现以下目标:

self.unique_characters = sorted(self.df.index.values.unique())

由于您的问题无法重现,因此我的回答基于我对该问题的一般评估。如果这不能解决问题,请发表评论。