Skip to content

Fixes dimension order in xarray.Dataset.to_stacked_array #10205

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

Merged
merged 5 commits into from
Apr 8, 2025
Merged
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
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ Deprecations
Bug fixes
~~~~~~~~~

- :py:meth:`~xarray.Dataset.to_stacked_array` now uses dimensions in order of appearance.
This fixes the issue where using :py:meth:`~xarray.Dataset.transpose` before :py:meth:`~xarray.Dataset.to_stacked_array`
had no effect. (Mentioned in :issue:`9921`)

Documentation
~~~~~~~~~~~~~
Expand Down
8 changes: 7 additions & 1 deletion xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -5246,7 +5246,13 @@ def to_stacked_array(
"""
from xarray.structure.concat import concat

stacking_dims = tuple(dim for dim in self.dims if dim not in sample_dims)
# add stacking dims by order of appearance
stacking_dims_list: list[Hashable] = []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor comment: This could also be a xarray.core.utils.OrderedSet

for da in self.data_vars.values():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late review @aFarchi . We will need to iterate over self.coords too. We can have coords variables with dimensions not present on any data_var

for dim in da.dims:
if dim not in sample_dims and dim not in stacking_dims_list:
stacking_dims_list.append(dim)
stacking_dims = tuple(stacking_dims_list)

for key, da in self.data_vars.items():
missing_sample_dims = set(sample_dims) - set(da.dims)
Expand Down
27 changes: 27 additions & 0 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4098,6 +4098,33 @@ def test_to_stacked_array_preserves_dtype(self) -> None:
expected_stacked_variable,
)

def test_to_stacked_array_transposed(self) -> None:
# test that to_stacked_array uses updated dim order after transposition
ds = xr.Dataset(
data_vars=dict(
v1=(["d1", "d2"], np.arange(6).reshape((2, 3))),
),
coords=dict(
d1=(["d1"], np.arange(2)),
d2=(["d2"], np.arange(3)),
),
)
da = ds.to_stacked_array(
new_dim="new_dim",
sample_dims=[],
variable_dim="variable",
)
dsT = ds.transpose()
daT = dsT.to_stacked_array(
new_dim="new_dim",
sample_dims=[],
variable_dim="variable",
)
v1 = np.arange(6)
v1T = np.arange(6).reshape((2, 3)).T.flatten()
np.testing.assert_equal(da.to_numpy(), v1)
np.testing.assert_equal(daT.to_numpy(), v1T)

def test_update(self) -> None:
data = create_test_data(seed=0)
expected = data.copy()
Expand Down
Loading