Skip to content

Commit 84df75d

Browse files
authored
Expand user dir paths (~) in open_mfdataset and to_zarr. (#4795)
* Normalize wildcard paths in open_mfdataset. * Document normalized mfdataset paths in what's new. * Also normalize paths in to_zarr.
1 parent 800ccb3 commit 84df75d

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

doc/whats-new.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ Bug fixes
7575
- Add ``missing_dims`` parameter to transpose (:issue:`4647`, :pull:`4767`). By `Daniel Mesejo <https://github.com/mesejo>`_.
7676
- Resolve intervals before appending other metadata to labels when plotting (:issue:`4322`, :pull:`4794`).
7777
By `Justus Magin <https://github.com/keewis>`_.
78+
- Expand user directory paths (e.g. ``~/``) in :py:func:`open_mfdataset` and
79+
:py:meth:`Dataset.to_zarr` (:issue:`4783`, :pull:`4795`).
80+
By `Julien Seguinot <https://github.com/juseg>`_.
7881

7982
Documentation
8083
~~~~~~~~~~~~~

xarray/backends/api.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,7 @@ def open_mfdataset(
887887
paths
888888
)
889889
)
890-
paths = sorted(glob(paths))
890+
paths = sorted(glob(_normalize_path(paths)))
891891
else:
892892
paths = [str(p) if isinstance(p, Path) else p for p in paths]
893893

@@ -1386,10 +1386,11 @@ def to_zarr(
13861386
13871387
See `Dataset.to_zarr` for full API docs.
13881388
"""
1389-
if isinstance(store, Path):
1390-
store = str(store)
1391-
if isinstance(chunk_store, Path):
1392-
chunk_store = str(store)
1389+
1390+
# expand str and Path arguments
1391+
store = _normalize_path(store)
1392+
chunk_store = _normalize_path(chunk_store)
1393+
13931394
if encoding is None:
13941395
encoding = {}
13951396

@@ -1419,9 +1420,6 @@ def to_zarr(
14191420
"compute=False before writing data."
14201421
)
14211422

1422-
if isinstance(store, Path):
1423-
store = str(store)
1424-
14251423
# validate Dataset keys, DataArray names, and attr keys/values
14261424
_validate_dataset_names(dataset)
14271425
_validate_attrs(dataset)

0 commit comments

Comments
 (0)