Skip to content

Commit 47f9253

Browse files
pmav99Illviljandcherian
authored andcommitted
Handle single PathLike objects in open_mfdataset() (pydata#5884)
* Handle single `PathLike` objects in `open_mfdataset()` Fixes pydata#5881 * Update doc/whats-new.rst Co-authored-by: Illviljan <[email protected]> Co-authored-by: Deepak Cherian <[email protected]>
1 parent 5a0969e commit 47f9253

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ Bug fixes
8080
By `Jimmy Westling <https://github.com/illviljan>`_.
8181
- Numbers are properly formatted in a plot's title (:issue:`5788`, :pull:`5789`).
8282
By `Maxime Liquet <https://github.com/maximlt>`_.
83+
- ``open_mfdataset()`` now accepts a single ``pathlib.Path`` object (:issue: `5881`).
84+
By `Panos Mavrogiorgos <https://github.com/pmav99>`_.
8385

8486
Documentation
8587
~~~~~~~~~~~~~

xarray/backends/api.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,8 @@ def open_mfdataset(
865865
)
866866
else:
867867
paths = sorted(glob(_normalize_path(paths)))
868+
elif isinstance(paths, os.PathLike):
869+
paths = [os.fspath(paths)]
868870
else:
869871
paths = [str(p) if isinstance(p, Path) else p for p in paths]
870872

xarray/tests/test_backends.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3039,6 +3039,14 @@ def test_open_mfdataset_manyfiles(
30393039
assert_identical(original, actual)
30403040

30413041

3042+
@requires_netCDF4
3043+
@requires_dask
3044+
def test_open_mfdataset_can_open_path_objects():
3045+
dataset = os.path.join(os.path.dirname(__file__), "data", "example_1.nc")
3046+
with open_mfdataset(Path(dataset)) as actual:
3047+
assert isinstance(actual, Dataset)
3048+
3049+
30423050
@requires_netCDF4
30433051
@requires_dask
30443052
def test_open_mfdataset_list_attr():

0 commit comments

Comments
 (0)