Description
As @Hoeze notes in #5179, calling stack()
can be "incredibly slow and memory-demanding, since it creates a MultiIndex of every possible coordinate in the array."
This is true with how stack()
works currently, but I'm not sure this is necessary. I suspect it's a vestigial design choice from copying pandas, back from before Xarray had optional indexes. One benefit is that it's convenient for making unstack()
the inverse of stack()
, but isn't always required.
Regardless of how we define the semantics for boolean indexing (#1887), it seems like it could be a good idea to allow stack to skip creating a MultiIndex for the new dimension, via a new keyword argument such as ds.stack(index=False)
. This would be equivalent to calling reset_index()
after stack()
but would be cheaper because the MultiIndex is never created in the first place.