Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undeprecate dict argument for coords in DataArray (#5527) #5528

Merged
merged 4 commits into from
Jul 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also add a test for 2D dataarrays?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! I'll add one asap


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