From 84df75d366edaaa0af172047145a3409cac9bb3a Mon Sep 17 00:00:00 2001 From: Julien Seguinot Date: Fri, 15 Jan 2021 18:22:27 +0100 Subject: [PATCH] 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. --- doc/whats-new.rst | 3 +++ xarray/backends/api.py | 14 ++++++-------- 2 files changed, 9 insertions(+), 8 deletions(-) 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)