Home > Software engineering >  TypeError: expected str, bytes or os.PathLike object, not Series
TypeError: expected str, bytes or os.PathLike object, not Series

Time:12-15

I was trying to solve a problem online with google colab. Here is the code cell.

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()

and here is the error I got,

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

Is there any easy solution?

CodePudding user response:

You are trying to combine a pandas series with pathlib.Path join features, which is not designed for that purpose (hence the error message "TypeError: expected [...], not Series")

A solution to this would be to extract the relevant cell from the dataframe before trying to combine it into a path. I do not know whether "band" and "chip_id" are connected or not, so let me give you an example where you just iterate ober all chip_ids (which might not be what you intended, but which should get rid of the error)


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()
  • Related