Skip to content

Faster dask unstack #6150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4196,20 +4196,15 @@ def unstack(
self.variables[k] for k in set(self.variables) - set(self.xindexes)
]
# Notes for each of these cases:
# 1. Dask arrays don't support assignment by index, which the fast unstack
# function requires.
# https://github.com/pydata/xarray/pull/4746#issuecomment-753282125
# 2. Sparse doesn't currently support (though we could special-case it)
# 1. Sparse doesn't currently support (though we could special-case it)
# https://github.com/pydata/sparse/issues/422
# 3. pint requires checking if it's a NumPy array until
# 2. pint requires checking if it's a NumPy array until
# https://github.com/pydata/xarray/pull/4751 is resolved,
# Once that is resolved, explicitly exclude pint arrays.
# pint doesn't implement `np.full_like` in a way that's
# currently compatible.
needs_full_reindex = any(
is_duck_dask_array(v.data)
or isinstance(v.data, sparse_array_type)
or not isinstance(v.data, np.ndarray)
isinstance(v.data, sparse_array_type) or not isinstance(v.data, np.ndarray)
for v in nonindexes
)

Expand Down
14 changes: 10 additions & 4 deletions xarray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -1659,11 +1659,11 @@ def _unstack_once(
else:
dtype = self.dtype

if sparse:
if sparse and not is_duck_dask_array(reordered):
# unstacking a dense multitindexed array to a sparse array
from sparse import COO

codes = zip(*index.codes)
codes = zip(*indexer)
if reordered.ndim == 1:
indexes = codes
else:
Expand All @@ -1680,17 +1680,23 @@ def _unstack_once(
)

else:
# dask supports assigning to a list of ints along one axis only.
# So we construct an array with the last dimension flattened,
# assign the values, then reshape to the final shape.
intermediate_shape = reordered.shape[:-1] + (np.prod(new_dim_sizes),)
indexer = np.ravel_multi_index(indexer, new_dim_sizes)
data = np.full_like(
self.data,
fill_value=fill_value,
shape=new_shape,
shape=intermediate_shape,
dtype=dtype,
)

# Indexer is a list of lists of locations. Each list is the locations
# on the new dimension. This is robust to the data being sparse; in that
# case the destinations will be NaN / zero.
data[(..., *indexer)] = reordered
data[(..., indexer)] = reordered
data = data.reshape(new_shape)

return self._replace(dims=new_dims, data=data)

Expand Down