【问题标题】:TypeError: expected str, bytes or os.PathLike object, not SeriesTypeError:预期的 str、bytes 或 os.PathLike 对象,而不是 Series
【发布时间】:2022-01-17 11:44:29
【问题描述】:

我试图通过 google colab 在线解决问题。 这是代码单元格。

def add_paths(df, feature_dir, label_dir=None, bands=BANDS):
  for band in bands:
      df[f"{band}_path"] = feature_dir / df["chip_id"] / f"{band}.tif"
      assert df[f"{band}_path"].path.exists().all()
  if label_dir is not None:
      df["label_path"] = label_dir / (df["chip_id"] + ".tif")
      assert df["label_path"].path.exists().all()

  return df

train_meta = add_paths(train_meta, TRAIN_FEATURES, TRAIN_LABELS)
train_meta.head()

这是我得到的错误,

TypeError                                 Traceback (most recent call last)
<ipython-input-46-db866ed40c79> in <module>()
     16 
     17 
---> 18 train_meta = add_paths(train_meta, TRAIN_FEATURES, TRAIN_LABELS)
     19 train_meta.head()

3 frames
/usr/lib/python3.7/pathlib.py in _parse_args(cls, args)
    656                 parts += a._parts
    657             else:
--> 658                 a = os.fspath(a)
    659                 if isinstance(a, str):
    660                     # Force-cast str subclasses to str (issue #21127)

TypeError: expected str, bytes or os.PathLike object, not Series

有什么简单的解决方法吗?

【问题讨论】:

    标签: python pandas dataframe series


    【解决方案1】:

    您正在尝试将 pandas 系列与 pathlib.Path 连接功能相结合,这不是为此目的而设计的(因此出现错误消息“TypeError: expected [...], not Series” )

    对此的解决方案是在尝试将其组合成路径之前从数据框中提取相关单元格。我不知道“band”和“chip_id”是否连接,所以让我举个例子,你只需迭代所有chip_ids(这可能不是你想要的,但应该摆脱错误)

    
    def add_paths(df, feature_dir, label_dir=None, bands=BANDS):
      for band in bands:
          band_paths = []
          for chip_id in df.chip_id:
              current_path = feature_dir / chip_id / f"{band}.tif"
              band_paths.append(current_path)
              assert current_path.is_file()
          df[f"{band}_path"] = band_paths
      if label_dir is not None:
          label_paths = []
          for chip_id in df.chip_id:
              current_path = label_dir / (chip_id + ".tif")
              assert current_path.is_file()
          df["label_path"] = label_paths
      return df
    
    train_meta = add_paths(train_meta, TRAIN_FEATURES, TRAIN_LABELS)
    train_meta.head()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-15
      • 2018-11-08
      • 2018-11-11
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多