Skip to content

Commit 8090513

Browse files
authored
Undeprecate dict argument for coords in DataArray (#5527) (#5528)
1 parent 3d1d134 commit 8090513

File tree

4 files changed

+17
-16
lines changed

4 files changed

+17
-16
lines changed

doc/whats-new.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ v0.18.3 (unreleased)
2121

2222
New Features
2323
~~~~~~~~~~~~
24-
24+
- Allow passing a dictionary as coords to a :py:class:`DataArray` (:issue:`5527`,
25+
reverts :pull:`1539`, which had deprecated this due to python's inconsistent ordering in earlier versions).
26+
By `Sander van Rijn <https://github.com/sjvrijn>`_.
2527
- Added :py:meth:`Dataset.coarsen.construct`, :py:meth:`DataArray.coarsen.construct` (:issue:`5454`, :pull:`5475`).
2628
By `Deepak Cherian <https://github.com/dcherian>`_.
2729
- Xarray now uses consolidated metadata by default when writing and reading Zarr

xarray/core/dataarray.py

+7-11
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,11 @@ def _infer_coords_and_dims(
117117
if coords is not None and len(coords) == len(shape):
118118
# try to infer dimensions from coords
119119
if utils.is_dict_like(coords):
120-
# deprecated in GH993, removed in GH1539
121-
raise ValueError(
122-
"inferring DataArray dimensions from "
123-
"dictionary like ``coords`` is no longer "
124-
"supported. Use an explicit list of "
125-
"``dims`` instead."
126-
)
127-
for n, (dim, coord) in enumerate(zip(dims, coords)):
128-
coord = as_variable(coord, name=dims[n]).to_index_variable()
129-
dims[n] = coord.name
120+
dims = list(coords.keys())
121+
else:
122+
for n, (dim, coord) in enumerate(zip(dims, coords)):
123+
coord = as_variable(coord, name=dims[n]).to_index_variable()
124+
dims[n] = coord.name
130125
dims = tuple(dims)
131126
elif len(dims) != len(shape):
132127
raise ValueError(
@@ -281,7 +276,8 @@ class DataArray(AbstractArray, DataWithCoords, DataArrayArithmetic):
281276
Name(s) of the data dimension(s). Must be either a hashable
282277
(only for 1D data) or a sequence of hashables with length equal
283278
to the number of dimensions. If this argument is omitted,
284-
dimension names default to ``['dim_0', ... 'dim_n']``.
279+
dimension names are taken from ``coords`` (if possible) and
280+
otherwise default to ``['dim_0', ... 'dim_n']``.
285281
name : str or None, optional
286282
Name of this array.
287283
attrs : dict_like or None, optional

xarray/tests/test_dataarray.py

+7
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,9 @@ def test_constructor(self):
304304
actual = DataArray(data, coords, ["x", "y"])
305305
assert_identical(expected, actual)
306306

307+
actual = DataArray(data, coords)
308+
assert_identical(expected, actual)
309+
307310
coords = [("x", ["a", "b"]), ("y", [-1, -2, -3])]
308311
actual = DataArray(data, coords)
309312
assert_identical(expected, actual)
@@ -332,6 +335,10 @@ def test_constructor(self):
332335
expected = Dataset({None: (["x", "y"], data, {}, {"bar": 2})})[None]
333336
assert_identical(expected, actual)
334337

338+
actual = DataArray([1, 2, 3], coords={"x": [0, 1, 2]})
339+
expected = DataArray([1, 2, 3], coords=[("x", [0, 1, 2])])
340+
assert_identical(expected, actual)
341+
335342
def test_constructor_invalid(self):
336343
data = np.random.randn(3, 2)
337344

xarray/tests/test_dataset.py

-4
Original file line numberDiff line numberDiff line change
@@ -419,10 +419,6 @@ class Arbitrary:
419419
actual = Dataset({"x": arg})
420420
assert_identical(expected, actual)
421421

422-
def test_constructor_deprecated(self):
423-
with pytest.raises(ValueError, match=r"DataArray dimensions"):
424-
DataArray([1, 2, 3], coords={"x": [0, 1, 2]})
425-
426422
def test_constructor_auto_align(self):
427423
a = DataArray([1, 2], [("x", [0, 1])])
428424
b = DataArray([3, 4], [("x", [1, 2])])

0 commit comments

Comments
 (0)