# xarray.core.groupby.DatasetGroupBy.shuffle_to_chunks

#### DatasetGroupBy.shuffle_to_chunks(chunks=None)[[source]](https://github.com/pydata/xarray/blob/main/xarray/core/groupby.py#L715-L765)

Sort or “shuffle” the underlying object.

“Shuffle” means the object is sorted so that all group members occur sequentially,
in the same chunk. Multiple groups may occur in the same chunk.
This method is particularly useful for chunked arrays (e.g. dask, cubed).
particularly when you need to map a function that requires all members of a group
to be present in a single chunk. For chunked array types, the order of appearance
is not guaranteed, but will depend on the input chunking.

* **Parameters:**
  **chunks** ([`int`](https://docs.python.org/3/builtins/functions.html#int), [`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple) of [`int`](https://docs.python.org/3/builtins/functions.html#int), `"auto"` or [mapping](https://docs.python.org/3/glossary.html#term-mapping) of [hashable](../user-guide/terminology.html.md#term-name) to [`int`](https://docs.python.org/3/builtins/functions.html#int) or [`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple) of [`int`](https://docs.python.org/3/builtins/functions.html#int), *optional*) – How to adjust chunks along dimensions not present in the array being grouped by.
* **Returns:**
  [`DataArrayGroupBy`](xarray.core.groupby.DataArrayGroupBy.html.md#xarray.core.groupby.DataArrayGroupBy) or [`DatasetGroupBy`](xarray.core.groupby.DatasetGroupBy.html.md#xarray.core.groupby.DatasetGroupBy)

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('e36b9cda-dbb6-4c51-b2f4-ce6bb12844be','33704f8c-bb5f-4993-b8d8-3f5e0cd0b63c','5674472f-6d9d-4124-9add-cc52070e605d','../lite/tree/../notebooks/index.html?path=79875507_ae9d_491e_bdcf_ae5e3c70f78a.ipynb','None')">Try it in your browser!</button></div>
```pycon
>>> import dask.array
>>> da = xr.DataArray(
...     dims="x",
...     data=dask.array.arange(10, chunks=3),
...     coords={"x": [1, 2, 3, 1, 2, 3, 1, 2, 3, 0]},
...     name="a",
... )
>>> shuffled = da.groupby("x").shuffle_to_chunks()
>>> shuffled
<xarray.DataArray 'a' (x: 10)> Size: 80B
dask.array<shuffle, shape=(10,), dtype=int64, chunksize=(3,), chunktype=numpy.ndarray>
Coordinates:
  * x        (x) int64 80B 0 1 1 1 2 2 2 3 3 3
```

```pycon
>>> shuffled.groupby("x").quantile(q=0.5).compute()
<xarray.DataArray 'a' (x: 4)> Size: 32B
array([9., 3., 4., 5.])
Coordinates:
  * x         (x) int64 32B 0 1 2 3
    quantile  float64 8B 0.5
```

<div id="5674472f-6d9d-4124-9add-cc52070e605d" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('e36b9cda-dbb6-4c51-b2f4-ce6bb12844be','5674472f-6d9d-4124-9add-cc52070e605d')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('e36b9cda-dbb6-4c51-b2f4-ce6bb12844be','5674472f-6d9d-4124-9add-cc52070e605d')">Open In Tab</button></div><div id="33704f8c-bb5f-4993-b8d8-3f5e0cd0b63c" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>

#### SEE ALSO
[`dask.dataframe.DataFrame.shuffle`](https://docs.dask.org/en/latest/generated/dask.dataframe.DataFrame.shuffle.html#dask.dataframe.DataFrame.shuffle), [`dask.array.shuffle`](https://docs.dask.org/en/latest/generated/dask.array.shuffle.html#dask.array.shuffle)
