Skip to content

Commit 690bde8

Browse files
committed
change return type of DataArray.chunks and Dataset.chunks
1 parent 5499949 commit 690bde8

File tree

4 files changed

+58
-23
lines changed

4 files changed

+58
-23
lines changed

xarray/core/common.py

+17
Original file line numberDiff line numberDiff line change
@@ -1813,6 +1813,23 @@ def ones_like(other, dtype: DTypeLike = None):
18131813
return full_like(other, 1, dtype)
18141814

18151815

1816+
def get_chunks(
1817+
variables: Iterable[Variable],
1818+
) -> Mapping[Hashable, Tuple[int, ...]]:
1819+
1820+
chunks: Dict[Hashable, Tuple[int, ...]] = {}
1821+
for v in variables:
1822+
if hasattr(v.data, "chunks"):
1823+
for dim, c in v.chunks.items():
1824+
if dim in chunks and c != chunks[dim]:
1825+
raise ValueError(
1826+
f"Object has inconsistent chunks along dimension {dim}. "
1827+
"This can be fixed by calling unify_chunks()."
1828+
)
1829+
chunks[dim] = c
1830+
return Frozen(chunks)
1831+
1832+
18161833
def is_np_datetime_like(dtype: DTypeLike) -> bool:
18171834
"""Check if a dtype is a subclass of the numpy datetime types"""
18181835
return np.issubdtype(dtype, np.datetime64) or np.issubdtype(dtype, np.timedelta64)

xarray/core/dataarray.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
reindex_like_indexers,
4444
)
4545
from .arithmetic import DataArrayArithmetic
46-
from .common import AbstractArray, DataWithCoords
46+
from .common import AbstractArray, DataWithCoords, get_chunks
4747
from .computation import unify_chunks
4848
from .coordinates import (
4949
DataArrayCoordinates,
@@ -1057,11 +1057,20 @@ def __deepcopy__(self, memo=None) -> "DataArray":
10571057
__hash__ = None # type: ignore[assignment]
10581058

10591059
@property
1060-
def chunks(self) -> Optional[Tuple[Tuple[int, ...], ...]]:
1061-
"""Block dimensions for this array's data or None if it's not a dask
1062-
array.
1060+
def chunks(self) -> Optional[Mapping[Hashable, Tuple[int, ...]]]:
10631061
"""
1064-
return self.variable.chunks
1062+
Mapping from dimension names to block lengths for this dataarray's data, or None if
1063+
the underlying data is not a dask array.
1064+
1065+
Cannot be modified directly, but can be modified by calling .chunk().
1066+
1067+
See Also
1068+
--------
1069+
DataArray.chunk
1070+
xarray.unify_chunks
1071+
"""
1072+
all_variables = [self.variable] + [c.variable for c in self.coords.values()]
1073+
return get_chunks(all_variables)
10651074

10661075
def chunk(
10671076
self,

xarray/core/dataset.py

+12-14
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
)
5353
from .alignment import _broadcast_helper, _get_broadcast_dims_map_common_coords, align
5454
from .arithmetic import DatasetArithmetic
55-
from .common import DataWithCoords, _contains_datetime_like_objects
55+
from .common import DataWithCoords, _contains_datetime_like_objects, get_chunks
5656
from .computation import unify_chunks
5757
from .coordinates import (
5858
DatasetCoordinates,
@@ -2090,20 +2090,18 @@ def info(self, buf=None) -> None:
20902090

20912091
@property
20922092
def chunks(self) -> Mapping[Hashable, Tuple[int, ...]]:
2093-
"""Block dimensions for this dataset's data or None if it's not a dask
2094-
array.
20952093
"""
2096-
chunks: Dict[Hashable, Tuple[int, ...]] = {}
2097-
for v in self.variables.values():
2098-
if v.chunks is not None:
2099-
for dim, c in zip(v.dims, v.chunks):
2100-
if dim in chunks and c != chunks[dim]:
2101-
raise ValueError(
2102-
f"Object has inconsistent chunks along dimension {dim}. "
2103-
"This can be fixed by calling unify_chunks()."
2104-
)
2105-
chunks[dim] = c
2106-
return Frozen(chunks)
2094+
Mapping from dimension names to block lengths for this dataset's data, or None if
2095+
the underlying data is not a dask array.
2096+
2097+
Cannot be modified directly, but can be modified by calling .chunk().
2098+
2099+
See Also
2100+
--------
2101+
Dataset.chunk
2102+
xarray.unify_chunks
2103+
"""
2104+
return get_chunks(self.variables.values())
21072105

21082106
def chunk(
21092107
self,

xarray/core/variable.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
sparse_array_type,
4646
)
4747
from .utils import (
48+
Frozen,
4849
NdimSizeLenMixin,
4950
OrderedSet,
5051
_default,
@@ -997,15 +998,25 @@ def __deepcopy__(self, memo=None):
997998

998999
@property
9991000
def chunks(self):
1000-
"""Block dimensions for this array's data or None if it's not a dask
1001-
array.
10021001
"""
1003-
return getattr(self._data, "chunks", None)
1002+
Mapping from dimension names to block lengths for this array's data, or None if
1003+
the underlying data is not a dask array.
1004+
1005+
Cannot be modified directly, but can be modified by calling .chunk().
1006+
1007+
See Also
1008+
--------
1009+
Variable.chunk
1010+
"""
1011+
if hasattr(self._data, "chunks"):
1012+
return Frozen({dim: c for dim, c in zip(self.dims, self.data.chunks)})
1013+
else:
1014+
return None
10041015

10051016
_array_counter = itertools.count()
10061017

10071018
def chunk(self, chunks={}, name=None, lock=False):
1008-
"""Coerce this array's data into a dask arrays with the given chunks.
1019+
"""Coerce this array's data into a dask array with the given chunks.
10091020
10101021
If this variable is a non-dask array, it will be converted to dask
10111022
array. If it's a dask array, it will be rechunked to the given chunk

0 commit comments

Comments
 (0)