Skip to content
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

Fix to_unstacked_dataset for single dimension variables. #4094

Merged
merged 2 commits into from Jul 2, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Expand Up @@ -160,6 +160,8 @@ Bug fixes
By `Mathias Hauser <https://github.com/mathause>`_.
- Fix html repr in untrusted notebooks: fallback to plain text repr. (:pull:`4053`)
By `Benoit Bovy <https://github.com/benbovy>`_.
- Fix :py:meth:`DataArray.to_unstacked_dataset` for single-dimension variables. (:issue:`4049`)
By `Deepak Cherian <https://github.com/dcherian>`_
- Fix :py:func:`open_rasterio` for ``WarpedVRT`` with specified ``src_crs``. (:pull:`4104`)
By `Dave Cole <https://github.com/dtpc>`_.

Expand Down
2 changes: 1 addition & 1 deletion xarray/core/dataarray.py
Expand Up @@ -1961,7 +1961,7 @@ def to_unstacked_dataset(self, dim, level=0):
# pull variables out of datarray
data_dict = {}
for k in variables:
data_dict[k] = self.sel({variable_dim: k}).squeeze(drop=True)
data_dict[k] = self.sel({variable_dim: k}, drop=True).squeeze(drop=True)

# unstacked dataset
return Dataset(data_dict)
Expand Down
8 changes: 8 additions & 0 deletions xarray/tests/test_dataset.py
Expand Up @@ -3031,6 +3031,14 @@ def test_to_stacked_array_dtype_dims(self):
assert y.dims == ("x", "features")

def test_to_stacked_array_to_unstacked_dataset(self):

# single dimension: regression test for GH4049
arr = xr.DataArray(np.arange(3), coords=[("x", [0, 1, 2])])
data = xr.Dataset({"a": arr, "b": arr})
stacked = data.to_stacked_array("y", sample_dims=["x"])
unstacked = stacked.to_unstacked_dataset("y")
assert_identical(unstacked, data)

# make a two dimensional dataset
a, b = create_test_stacked_array()
D = xr.Dataset({"a": a, "b": b})
Expand Down