diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 21f453b6865..cd65e0dbe35 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -21,7 +21,9 @@ v0.18.3 (unreleased) New Features ~~~~~~~~~~~~ - +- Allow passing a dictionary as coords to a :py:class:`DataArray` (:issue:`5527`, + reverts :pull:`1539`, which had deprecated this due to python's inconsistent ordering in earlier versions). + By `Sander van Rijn `_. - Added :py:meth:`Dataset.coarsen.construct`, :py:meth:`DataArray.coarsen.construct` (:issue:`5454`, :pull:`5475`). By `Deepak Cherian `_. - Xarray now uses consolidated metadata by default when writing and reading Zarr diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index cfb7230d1ae..b4d553c235a 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -117,16 +117,11 @@ def _infer_coords_and_dims( if coords is not None and len(coords) == len(shape): # try to infer dimensions from coords if utils.is_dict_like(coords): - # deprecated in GH993, removed in GH1539 - raise ValueError( - "inferring DataArray dimensions from " - "dictionary like ``coords`` is no longer " - "supported. Use an explicit list of " - "``dims`` instead." - ) - for n, (dim, coord) in enumerate(zip(dims, coords)): - coord = as_variable(coord, name=dims[n]).to_index_variable() - dims[n] = coord.name + dims = list(coords.keys()) + else: + for n, (dim, coord) in enumerate(zip(dims, coords)): + coord = as_variable(coord, name=dims[n]).to_index_variable() + dims[n] = coord.name dims = tuple(dims) elif len(dims) != len(shape): raise ValueError( @@ -281,7 +276,8 @@ class DataArray(AbstractArray, DataWithCoords, DataArrayArithmetic): Name(s) of the data dimension(s). Must be either a hashable (only for 1D data) or a sequence of hashables with length equal to the number of dimensions. If this argument is omitted, - dimension names default to ``['dim_0', ... 'dim_n']``. + dimension names are taken from ``coords`` (if possible) and + otherwise default to ``['dim_0', ... 'dim_n']``. name : str or None, optional Name of this array. attrs : dict_like or None, optional diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 8a82c8c37f3..b9f04085935 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -304,6 +304,9 @@ def test_constructor(self): actual = DataArray(data, coords, ["x", "y"]) assert_identical(expected, actual) + actual = DataArray(data, coords) + assert_identical(expected, actual) + coords = [("x", ["a", "b"]), ("y", [-1, -2, -3])] actual = DataArray(data, coords) assert_identical(expected, actual) @@ -332,6 +335,10 @@ def test_constructor(self): expected = Dataset({None: (["x", "y"], data, {}, {"bar": 2})})[None] assert_identical(expected, actual) + actual = DataArray([1, 2, 3], coords={"x": [0, 1, 2]}) + expected = DataArray([1, 2, 3], coords=[("x", [0, 1, 2])]) + assert_identical(expected, actual) + def test_constructor_invalid(self): data = np.random.randn(3, 2) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 9a001117c59..b08ce9ea730 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -419,10 +419,6 @@ class Arbitrary: actual = Dataset({"x": arg}) assert_identical(expected, actual) - def test_constructor_deprecated(self): - with pytest.raises(ValueError, match=r"DataArray dimensions"): - DataArray([1, 2, 3], coords={"x": [0, 1, 2]}) - def test_constructor_auto_align(self): a = DataArray([1, 2], [("x", [0, 1])]) b = DataArray([3, 4], [("x", [1, 2])])