Skip to content

Commit

Permalink
Fix to_unstacked_dataset for single dimension variables. (#4094)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcherian committed Jul 2, 2020
1 parent 03d409e commit 329cefb
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Expand Up @@ -171,6 +171,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

0 comments on commit 329cefb

Please sign in to comment.