Skip to content

Commit

Permalink
Undeprecate dict argument for coords in DataArray (#5527) (#5528)
Browse files Browse the repository at this point in the history
  • Loading branch information
sjvrijn authored Jul 4, 2021
1 parent 3d1d134 commit 8090513
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
4 changes: 3 additions & 1 deletion doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/sjvrijn>`_.
- Added :py:meth:`Dataset.coarsen.construct`, :py:meth:`DataArray.coarsen.construct` (:issue:`5454`, :pull:`5475`).
By `Deepak Cherian <https://github.com/dcherian>`_.
- Xarray now uses consolidated metadata by default when writing and reading Zarr
Expand Down
18 changes: 7 additions & 11 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
4 changes: 0 additions & 4 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])])
Expand Down

0 comments on commit 8090513

Please sign in to comment.