尝试使用.iloc索引Pandas Dataframe时出错

时间:2018-11-20 01:33:41

标签: python pandas

我面临一个奇怪的问题,试图为我的熊猫数据框的一行建立索引。在下面的代码中,行self.df.iloc[index]引发以下错误。我打印了indexindex.numpy()len(self.df)的值以验证我没有超出范围。它们的值为tensor(640) 640 649。我尝试使用自己的值对数据框进行索引,该值有效,因此我不确定错误是什么。我也将其发布在PyTorch论坛上,因为我不确定错误在哪里。

  • 我没有超越界限
  • 我可以使用自己的值(例如640)建立索引
  • 如果我从张量转换为numpy,我仍然无法为其编制索引

错误消息

Traceback (most recent call last):   File "train.py", line 532, in <module>
    main(args)   File "train.py", line 203, in main
    for batch in train_loader:   File "/usr/local/lib/python3.5/dist-packages/torch/utils/data/dataloader.py", line 336, in __next__
    return self._process_next_batch(batch)   File "/usr/local/lib/python3.5/dist-packages/torch/utils/data/dataloader.py", line 357, in _process_next_batch
    raise batch.exc_type(batch.exc_msg) TypeError: Traceback (most recent call last):   File "/usr/local/lib/python3.5/dist-packages/torch/utils/data/dataloader.py", line 106, in _worker_loop
    samples = collate_fn([dataset[i] for i in batch_indices])   File "/usr/local/lib/python3.5/dist-packages/torch/utils/data/dataloader.py", line 106, in <listcomp>
    samples = collate_fn([dataset[i] for i in batch_indices])   File "/home/HaziqBinRazali03/Trajectory Prediction/sgan-original/sgan/data/trajectories.py", line 204, in
__getitem__
    df = self.df.iloc[b]   File "/usr/local/lib/python3.5/dist-packages/pandas/core/indexing.py", line 1478, in __getitem__
    return self._getitem_axis(maybe_callable, axis=axis)   File "/usr/local/lib/python3.5/dist-packages/pandas/core/indexing.py", line 2091, in _getitem_axis
    return self._get_list_axis(key, axis=axis)   File "/usr/local/lib/python3.5/dist-packages/pandas/core/indexing.py", line 2070, in _get_list_axis
    return self.obj._take(key, axis=axis)   File "/usr/local/lib/python3.5/dist-packages/pandas/core/generic.py", line 2789, in _take
    verify=True)   File "/usr/local/lib/python3.5/dist-packages/pandas/core/internals.py", line 4537, in take
    new_labels = self.axes[axis].take(indexer)   File "/usr/local/lib/python3.5/dist-packages/pandas/core/indexes/base.py", line 2195, in take
    return self._shallow_copy(taken)   File "/usr/local/lib/python3.5/dist-packages/pandas/core/indexes/range.py", line 267, in _shallow_copy
    return self._int64index._shallow_copy(values, **kwargs)   File "/usr/local/lib/python3.5/dist-packages/pandas/core/indexes/numeric.py", line 68, in _shallow_copy
    return self._shallow_copy_with_infer(values=values, **kwargs)   File "/usr/local/lib/python3.5/dist-packages/pandas/core/indexes/base.py", line 538, in _shallow_copy_with_infer
    if not len(values) and 'dtype' not in kwargs: TypeError: object of type 'numpy.int64' has no len()

代码段

   def __getitem__(self, index):

        print(index, index.numpy(), len(self.df))

        # get the row at index
        df = self.df.iloc[index] # doesnt work
        df = self.df.iloc[index.numpy()] # doesnt work
        df = self.df.iloc[640] # works

        # load the images and the label
        pedestrian_images = []
        pedestrian_label  = df["label"]
        for full_filename in df["full_filename"]:
            pedestrian_images.append(self.transform(Image.open(full_filename)))
        pedestrian_images = torch.stack(pedestrian_images, 0)

        return [pedestrian_images, pedestrian_label]

熊猫数据框的一部分

     unique_id  label                                      full_filename
0          112      0  [/home/HaziqBinRazali03/Trajectory Prediction/...
1          606      0  [/home/HaziqBinRazali03/Trajectory Prediction/...
2          327      0  [/home/HaziqBinRazali03/Trajectory Prediction/...
3          385      0  [/home/HaziqBinRazali03/Trajectory Prediction/...
4          736      0  [/home/HaziqBinRazali03/Trajectory Prediction/...
5          634      0  [/home/HaziqBinRazali03/Trajectory Prediction/...
6          534      0  [/home/HaziqBinRazali03/Trajectory Prediction/...
7           61      0  [/home/HaziqBinRazali03/Trajectory Prediction/...
8           40      0  [/home/HaziqBinRazali03/Trajectory Prediction/...
9          124      0  [/home/HaziqBinRazali03/Trajectory Prediction/...
10         165      0  [/home/HaziqBinRazali03/Trajectory Prediction/...
11          97      0  [/home/HaziqBinRazali03/Trajectory Prediction/...
12         559      0  [/home/HaziqBinRazali03/Trajectory Prediction/...
13          98      0  [/home/HaziqBinRazali03/Trajectory Prediction/...
14         190      0  [/home/HaziqBinRazali03/Trajectory Prediction/...
15         360      0  [/home/HaziqBinRazali03/Trajectory Prediction/...
16         478      0  [/home/HaziqBinRazali03/Trajectory Prediction/...
17         737      0  [/home/HaziqBinRazali03/Trajectory Prediction/...
18         633      0  [/home/HaziqBinRazali03/Trajectory Prediction/...
19         362      0  [/home/HaziqBinRazali03/Trajectory Prediction/...
20          95      0  [/home/HaziqBinRazali03/Trajectory Prediction/...
21         375      0  [/home/HaziqBinRazali03/Trajectory Prediction/...
22         301      0  [/home/HaziqBinRazali03/Trajectory Prediction/...
23         729      0  [/home/HaziqBinRazali03/Trajectory Prediction/...
24         240      0  [/home/HaziqBinRazali03/Trajectory Prediction/...
25         312      0  [/home/HaziqBinRazali03/Trajectory Prediction/...
26         537      0  [/home/HaziqBinRazali03/Trajectory Prediction/...
27         786      0  [/home/HaziqBinRazali03/Trajectory Prediction/...
28         585      0  [/home/HaziqBinRazali03/Trajectory Prediction/...
29         443      0  [/home/HaziqBinRazali03/Trajectory Prediction/...

0 个答案:

没有答案