From 287d70430da1380be15c3a03817067affb2900f9 Mon Sep 17 00:00:00 2001 From: Sander van Rijn Date: Thu, 24 Jun 2021 21:59:22 +0200 Subject: [PATCH 1/3] Undeprecate dict argument for coords in DataArray (#5527) --- doc/whats-new.rst | 2 ++ xarray/core/dataarray.py | 18 +++++++----------- xarray/tests/test_dataarray.py | 4 ++++ xarray/tests/test_dataset.py | 4 ---- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index e0b1a850744..0da800c0d9e 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -21,6 +21,8 @@ v0.18.3 (unreleased) New Features ~~~~~~~~~~~~ +- Undeprecate passing a dictionary as coords to a :py:class:`DataArray` (:issue:`5527`, + reverts :pull:`1539`). By `Sander van Rijn `_. - Allow assigning values to a subset of a dataset using positional or label-based indexing (:issue:`3015`, :pull:`5362`). By `Matthias Göbel `_. - Attempting to reduce a weighted object over missing dimensions now raises an error (:pull:`5362`). diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index 2ab60b894e1..ffaf8389fc6 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -111,16 +111,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( @@ -275,7 +270,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 4a5cf39ab8c..0f9d3746241 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -332,6 +332,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 00a6fb825c7..6836fe4817d 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -445,10 +445,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])]) From 0c44fcf1db5479a4d932731cd585b0f4c513c876 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Mon, 28 Jun 2021 14:29:27 -0700 Subject: [PATCH 2/3] lint --- doc/whats-new.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index c14ad08b7d0..0b4fd0fcdbe 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -22,7 +22,7 @@ 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). + 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 `_. From 5d77f5774fe1530d2ea4b87e8274db9fd0a3c2c0 Mon Sep 17 00:00:00 2001 From: Sander van Rijn Date: Sun, 4 Jul 2021 10:10:54 +0200 Subject: [PATCH 3/3] Add 2d test for coords as dict without explicit dims --- xarray/tests/test_dataarray.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 33b32529def..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)