Skip to content

open_mfdataset does not handle single pathlib.Path objects but handles single str just fine #5881

Closed
@pmav99

Description

@pmav99

What happened:

An exception was raised

What you expected to happen:

That a dataset object would be created.

Minimal Complete Verifiable Example:

from pathlib import Path

import xarray as xr

dataset = "xarray/tests/data/example_1.nc"

with xr.open_mfdataset(dataset) as actual:
    assert isinstance(actual, xr.Dataset)

with xr.open_mfdataset(Path(dataset)) as actual:
    assert isinstance(actual, xr.Dataset)  # this fails

with the following traceback

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/panos/Prog/git/xarray/xarray/backends/api.py", line 869, in open_mfdataset
    paths = [str(p) if isinstance(p, Path) else p for p in paths]
TypeError: 'PosixPath' object is not iterable

Anything else we need to know?:

This seems to be fixing it, but I am not familiar with the codebase to know if this is the proper way to fix it

diff --git a/xarray/backends/api.py b/xarray/backends/api.py
index 6d73946b..596ac249 100644
--- a/xarray/backends/api.py
+++ b/xarray/backends/api.py
@@ -838,6 +838,8 @@ def open_mfdataset(
     .. [1] http://xarray.pydata.org/en/stable/dask.html
     .. [2] http://xarray.pydata.org/en/stable/dask.html#chunking-and-performance
     """
+    if isinstance(paths, Path):
+        paths = paths.as_posix()
     if isinstance(paths, str):
         if is_remote_uri(paths) and engine == "zarr":
             try:

Environment:

Output of xr.show_versions()
>>> xr.show_versions()

INSTALLED VERSIONS
------------------
commit: 07de257c5884df49335496ee6347fb633a7c302c
python: 3.9.7 (default, Aug 31 2021, 13:28:12) 
[GCC 11.1.0]
python-bits: 64
OS: Linux
OS-release: 5.14.12-arch1-1
machine: x86_64
processor: 
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.12.0
libnetcdf: 4.7.4

xarray: 999
pandas: 1.3.4
numpy: 1.21.3
scipy: None
netCDF4: 1.5.7
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: 1.5.1
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: 2021.09.1
distributed: None
matplotlib: None
cartopy: None
seaborn: None
numbagg: None
fsspec: 2021.10.1
cupy: None
pint: None
sparse: None
setuptools: 57.4.0
pip: 21.3
conda: None
pytest: 6.2.5
IPython: None
sphinx: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions