Skip to content

Commit fa201bd

Browse files
committed
cleanups + add comments
1 parent e976ada commit fa201bd

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

xarray/core/variable.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1573,14 +1573,13 @@ def _unstack_once(
15731573
"""
15741574

15751575
reordered = self.transpose(..., dim)
1576-
shape = reordered.shape
15771576
new_dim_sizes = [lev.size for lev in index.levels]
15781577
new_dim_names = index.names
15791578
indexer = index.codes
15801579

15811580
# Potentially we could replace `len(other_dims)` with just `-1`
15821581
other_dims = [d for d in self.dims if d != dim]
1583-
new_shape = tuple(list(shape[: len(other_dims)]) + new_dim_sizes)
1582+
new_shape = tuple(list(reordered.shape[: len(other_dims)]) + new_dim_sizes)
15841583
new_dims = reordered.dims[: len(other_dims)] + new_dim_names
15851584

15861585
if fill_value is dtypes.NA:
@@ -1594,14 +1593,17 @@ def _unstack_once(
15941593
dtype = self.dtype
15951594

15961595
if sparse:
1596+
# unstacking a dense multitindexed array to a sparse array
1597+
# Use the sparse.COO constructor until sparse supports advanced indexing
1598+
# https://github.com/pydata/sparse/issues/114
15971599
# TODO: how do we allow different sparse array types
15981600
from sparse import COO
15991601

16001602
codes = zip(*index.codes)
1601-
if not shape[:-1]:
1603+
if reordered.ndim == 1:
16021604
indexes = codes
16031605
else:
1604-
sizes = itertools.product(range(*shape[:-1]))
1606+
sizes = itertools.product(range(*reordered.shape[:-1]))
16051607
tuple_indexes = itertools.product(sizes, codes)
16061608
indexes = map(lambda x: list(itertools.chain(*x)), tuple_indexes) # type: ignore
16071609

0 commit comments

Comments
 (0)