diff --git a/doc/whats-new.rst b/doc/whats-new.rst index db10ec653c5..51b16d65a62 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -75,6 +75,9 @@ Bug fixes - Add ``missing_dims`` parameter to transpose (:issue:`4647`, :pull:`4767`). By `Daniel Mesejo `_. - Resolve intervals before appending other metadata to labels when plotting (:issue:`4322`, :pull:`4794`). By `Justus Magin `_. +- Expand user directory paths (e.g. ``~/``) in :py:func:`open_mfdataset` and + :py:meth:`Dataset.to_zarr` (:issue:`4783`, :pull:`4795`). + By `Julien Seguinot `_. Documentation ~~~~~~~~~~~~~ diff --git a/xarray/backends/api.py b/xarray/backends/api.py index faa7e6cf3d3..4958062a262 100644 --- a/xarray/backends/api.py +++ b/xarray/backends/api.py @@ -887,7 +887,7 @@ def open_mfdataset( paths ) ) - paths = sorted(glob(paths)) + paths = sorted(glob(_normalize_path(paths))) else: paths = [str(p) if isinstance(p, Path) else p for p in paths] @@ -1386,10 +1386,11 @@ def to_zarr( See `Dataset.to_zarr` for full API docs. """ - if isinstance(store, Path): - store = str(store) - if isinstance(chunk_store, Path): - chunk_store = str(store) + + # expand str and Path arguments + store = _normalize_path(store) + chunk_store = _normalize_path(chunk_store) + if encoding is None: encoding = {} @@ -1419,9 +1420,6 @@ def to_zarr( "compute=False before writing data." ) - if isinstance(store, Path): - store = str(store) - # validate Dataset keys, DataArray names, and attr keys/values _validate_dataset_names(dataset) _validate_attrs(dataset)