flox.xarray.rechunk_for_cohorts(obj, dim, labels, force_new_chunk_at, chunksize=None, ignore_old_chunks=False, debug=False)[source]

Rechunks array so that each new chunk contains groups that always occur together.

objDataArray or Dataset

array to rechunk


Dimension to rechunk


1D Group labels to align chunks with. This routine works well when labels has repeating patterns: e.g. 1, 2, 3, 1, 2, 3, 4, 1, 2, 3 though there is no requirement that the pattern must contain sequences.


Labels at which we always start a new chunk. For the example labels array, this would be 1.

chunksizeint, optional

nominal chunk size. Chunk size is exceeded when the label in force_new_chunk_at is less than chunksize//2 elements away. If None, uses median chunksize along dim.

DataArray or Dataset

Xarray object with rechunked arrays.