Skip to content

Commit

Permalink
Check for just ..., rather than [...] in da.stack (#6132)
Browse files Browse the repository at this point in the history
* Add tests for cases discussed in issue 6051

* Raise error if dims = ...
  • Loading branch information
StanczakDominik authored Jan 3, 2022
1 parent 0a40bf1 commit 60754fd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3870,6 +3870,8 @@ def reorder_levels(
return self._replace(variables, indexes=indexes)

def _stack_once(self, dims, new_dim):
if dims == ...:
raise ValueError("Please use [...] for dims, rather than just ...")
if ... in dims:
dims = list(infix_dims(dims, self.dims))
variables = {}
Expand Down
14 changes: 14 additions & 0 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -6678,3 +6678,17 @@ def test_from_pint_wrapping_dask(self):
expected = xr.DataArray(arr, dims="x", coords={"lat": ("x", arr * 2)})
assert_identical(result, expected)
np.testing.assert_equal(da.to_numpy(), arr)


class TestStackEllipsis:
# https://github.com/pydata/xarray/issues/6051
def test_result_as_expected(self):
da = DataArray([[1, 2], [1, 2]], dims=("x", "y"))
result = da.stack(flat=[...])
expected = da.stack(flat=da.dims)
assert_identical(result, expected)

def test_error_on_ellipsis_without_list(self):
da = DataArray([[1, 2], [1, 2]], dims=("x", "y"))
with pytest.raises(ValueError):
da.stack(flat=...)

0 comments on commit 60754fd

Please sign in to comment.