Skip to content

Commit

Permalink
Add dask specific kwargs to DataArray.chunk() (pydata#1432)
Browse files Browse the repository at this point in the history
  • Loading branch information
fmaussion authored May 30, 2017
1 parent 9287caa commit 8df660b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ By `Keisuke Fujii <https://github.com/fujiisoup>`_.
successful download (:issue:`1392`). By `Matthew Gidden
<https://github.com/gidden>`_.

- ``DataArray.chunk()`` now accepts dask specific kwargs like
``Dataset.chunk()`` does. By `Fabien Maussion <https://github.com/fmaussion>`_.

Documentation
~~~~~~~~~~~~~

Expand Down
13 changes: 11 additions & 2 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,8 @@ def chunks(self):
"""
return self.variable.chunks

def chunk(self, chunks=None):
def chunk(self, chunks=None, name_prefix='xarray-', token=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
Expand All @@ -647,6 +648,13 @@ def chunk(self, chunks=None):
chunks : int, tuple or dict, optional
Chunk sizes along each dimension, e.g., ``5``, ``(5, 5)`` or
``{'x': 5, 'y': 5}``.
name_prefix : str, optional
Prefix for the name of the new dask array.
token : str, optional
Token uniquely identifying this array.
lock : optional
Passed on to :py:func:`dask.array.from_array`, if the array is not
already as dask array.
Returns
-------
Expand All @@ -655,7 +663,8 @@ def chunk(self, chunks=None):
if isinstance(chunks, (list, tuple)):
chunks = dict(zip(self.dims, chunks))

ds = self._to_temp_dataset().chunk(chunks)
ds = self._to_temp_dataset().chunk(chunks, name_prefix=name_prefix,
token=token, lock=lock)
return self._from_temp_dataset(ds)

def isel(self, drop=False, **indexers):
Expand Down
6 changes: 6 additions & 0 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,12 @@ def test_chunk(self):

self.assertIsNone(blocked.load().chunks)

# Check that kwargs are passed
import dask.array as da
blocked = unblocked.chunk(name_prefix='testname_')
self.assertIsInstance(blocked.data, da.Array)
assert 'testname_' in blocked.data.name

def test_isel(self):
self.assertDataArrayIdentical(self.dv[0], self.dv.isel(x=0))
self.assertDataArrayIdentical(self.dv, self.dv.isel(x=slice(None)))
Expand Down

0 comments on commit 8df660b

Please sign in to comment.