From 4855248be3e7a75953bc6a0d56c41e85354ce51f Mon Sep 17 00:00:00 2001 From: Aureliana Barghini Date: Mon, 30 Nov 2020 18:44:43 +0100 Subject: [PATCH 1/3] change default in ds.chunk and datarray.chunk variable.chunk and add warning --- xarray/core/dataarray.py | 3 +-- xarray/core/dataset.py | 24 ++++++++++++++---------- xarray/core/variable.py | 15 ++++++++++----- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index b95f681bc79..4fcef4beb7f 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -1010,12 +1010,11 @@ def chunks(self) -> Optional[Tuple[Tuple[int, ...], ...]]: def chunk( self, chunks: Union[ - None, Number, Tuple[Number, ...], Tuple[Tuple[Number, ...], ...], Mapping[Hashable, Union[None, Number, Tuple[Number, ...]]], - ] = None, + ] = {}, name_prefix: str = "xarray-", token: str = None, lock: bool = False, diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 04974c58113..c77cfbcde97 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -362,7 +362,7 @@ def _assert_empty(args: tuple, msg: str = "%s") -> None: def _maybe_chunk( name, var, - chunks=None, + chunks, token=None, lock=None, name_prefix="xarray-", @@ -1819,11 +1819,10 @@ def chunks(self) -> Mapping[Hashable, Tuple[int, ...]]: def chunk( self, chunks: Union[ - None, Number, str, Mapping[Hashable, Union[None, Number, str, Tuple[Number, ...]]], - ] = None, + ] = {}, name_prefix: str = "xarray-", token: str = None, lock: bool = False, @@ -1855,17 +1854,22 @@ def chunk( ------- chunked : xarray.Dataset """ + if chunks is None: + warnings.warn( + "None value for 'chunks' is deprecated. " + "It will raise an error in the future. Use instead '{}'", + category=FutureWarning, + ) + chunks = {} if isinstance(chunks, (Number, str)): chunks = dict.fromkeys(self.dims, chunks) - if chunks is not None: - bad_dims = chunks.keys() - self.dims.keys() - if bad_dims: - raise ValueError( - "some chunks keys are not dimensions on this " - "object: %s" % bad_dims - ) + bad_dims = chunks.keys() - self.dims.keys() + if bad_dims: + raise ValueError( + "some chunks keys are not dimensions on this " "object: %s" % bad_dims + ) variables = { k: _maybe_chunk(k, v, chunks, token, lock, name_prefix) diff --git a/xarray/core/variable.py b/xarray/core/variable.py index a3876cb0077..a604a81cfa1 100644 --- a/xarray/core/variable.py +++ b/xarray/core/variable.py @@ -986,7 +986,7 @@ def chunks(self): _array_counter = itertools.count() - def chunk(self, chunks=None, name=None, lock=False): + def chunk(self, chunks={}, name=None, lock=False): """Coerce this array's data into a dask arrays with the given chunks. If this variable is a non-dask array, it will be converted to dask @@ -1016,12 +1016,17 @@ def chunk(self, chunks=None, name=None, lock=False): import dask import dask.array as da + if chunks is None: + warnings.warn( + "None value for 'chunks' is deprecated. " + "It will raise an error in the future. Use instead '{}'", + category=FutureWarning, + ) + chunks = {} + if utils.is_dict_like(chunks): chunks = {self.get_axis_num(dim): chunk for dim, chunk in chunks.items()} - if chunks is None: - chunks = self.chunks or self.shape - data = self._data if is_duck_dask_array(data): data = data.rechunk(chunks) @@ -2368,7 +2373,7 @@ def values(self, values): f"Please use DataArray.assign_coords, Dataset.assign_coords or Dataset.assign as appropriate." ) - def chunk(self, chunks=None, name=None, lock=False): + def chunk(self, chunks={}, name=None, lock=False): # Dummy - do not chunk. This method is invoked e.g. by Dataset.chunk() return self.copy(deep=False) From f17d6ae0b993fe8077c749ef55fe050d4416ba99 Mon Sep 17 00:00:00 2001 From: Aureliana Barghini Date: Wed, 9 Dec 2020 14:50:53 +0100 Subject: [PATCH 2/3] add comment for chunks={} in dataset.chunk and dataarray.chunk --- xarray/core/dataarray.py | 2 +- xarray/core/dataset.py | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index 4fcef4beb7f..c9baa143986 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -1014,7 +1014,7 @@ def chunk( Tuple[Number, ...], Tuple[Tuple[Number, ...], ...], Mapping[Hashable, Union[None, Number, Tuple[Number, ...]]], - ] = {}, + ] = {}, # {} even though it's technically unsafe, is being used intentionally here (#4667) name_prefix: str = "xarray-", token: str = None, lock: bool = False, diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index c77cfbcde97..ce27ebc56cc 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -1822,8 +1822,7 @@ def chunk( Number, str, Mapping[Hashable, Union[None, Number, str, Tuple[Number, ...]]], - ] = {}, - name_prefix: str = "xarray-", + ] = {}, # {} even though it's technically unsafe, is being used intentionally here (#4667) token: str = None, lock: bool = False, ) -> "Dataset": From ca987f1e6dcd027b65bea5aa35ed291611514286 Mon Sep 17 00:00:00 2001 From: Aureliana Barghini Date: Thu, 10 Dec 2020 08:54:45 +0100 Subject: [PATCH 3/3] fix: revert delete line --- xarray/core/dataset.py | 1 + 1 file changed, 1 insertion(+) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index ce27ebc56cc..fb158e2fe4f 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -1823,6 +1823,7 @@ def chunk( str, Mapping[Hashable, Union[None, Number, str, Tuple[Number, ...]]], ] = {}, # {} even though it's technically unsafe, is being used intentionally here (#4667) + name_prefix: str = "xarray-", token: str = None, lock: bool = False, ) -> "Dataset":