From 7a17e9f5a3cee02fa72b5970ab1176d84780efc0 Mon Sep 17 00:00:00 2001 From: Joe Hamman Date: Wed, 30 Aug 2017 16:39:28 -0700 Subject: [PATCH 1/2] remove warning and raise error when dataset constructor is called with coords dict and not dims --- doc/whats-new.rst | 9 ++++++++- xarray/core/dataarray.py | 13 +++++++------ xarray/tests/test_dataset.py | 8 +++++--- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 010f4856bb0..91f17cf9e0b 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -15,9 +15,16 @@ What's New .. _whats-new.0.9.7: -v0.9.7 (unreleased) +v0.10.0 (unreleased) ------------------- +Breaking changes +~~~~~~~~~~~~~~~~ + +- Supplying ``coords`` as a dictionary to the ``DataArray`` constructor without + also supplying an explicit ``dims`` argument is no longer supported. (:issue:`727`). + By `Joe Hamman `_. + Enhancements ~~~~~~~~~~~~ diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index 6b4d28e1006..c732efb71f1 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -47,14 +47,15 @@ def _infer_coords_and_dims(shape, coords, dims): if coords is not None and len(coords) == len(shape): # try to infer dimensions from coords if utils.is_dict_like(coords): - warnings.warn('inferring DataArray dimensions from dictionary ' - 'like ``coords`` has been deprecated. Use an ' - 'explicit list of ``dims`` instead.', - FutureWarning, stacklevel=3) - dims = list(coords.keys()) + # deprecated in GH993, removed in GH1539 + raise ValueError('inferring DataArray dimensions from ' + 'dictionary like ``coords`` has been ' + 'deprecated. Use an explicit list of ' + '``dims`` instead.') else: for n, (dim, coord) in enumerate(zip(dims, coords)): - coord = as_variable(coord, name=dims[n]).to_index_variable() + coord = as_variable(coord, + name=dims[n]).to_index_variable() dims[n] = coord.name dims = tuple(dims) else: diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index e6a91b103ed..52e46b50bac 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -274,7 +274,7 @@ class Arbitrary(object): self.assertDatasetIdentical(expected, actual) def test_constructor_deprecated(self): - with pytest.warns(FutureWarning): + with self.assertRaisesRegexp(ValueError, 'DataArray dimensions'): DataArray([1, 2, 3], coords={'x': [0, 1, 2]}) def test_constructor_auto_align(self): @@ -1294,9 +1294,11 @@ def test_align_nocopy(self): assert source_ndarray(x['foo'].data) is not source_ndarray(x2['foo'].data) def test_align_indexes(self): - x = Dataset({'foo': DataArray([1, 2, 3], coords=[('x', [1, 2, 3])])}) + x = Dataset({'foo': DataArray([1, 2, 3], dims='x', + coords=[('x', [1, 2, 3])])}) x2, = align(x, indexes={'x': [2, 3, 1]}) - expected_x2 = Dataset({'foo': DataArray([2, 3, 1], coords={'x': [2, 3, 1]})}) + expected_x2 = Dataset({'foo': DataArray([2, 3, 1], dims='x', + coords={'x': [2, 3, 1]})}) self.assertDatasetIdentical(expected_x2, x2) def test_align_non_unique(self): From 90ff7c185390fe93a19b84c63df0ad1c7fe321db Mon Sep 17 00:00:00 2001 From: Joe Hamman Date: Wed, 30 Aug 2017 22:33:54 -0700 Subject: [PATCH 2/2] remove else clause and improve whats new note --- doc/whats-new.rst | 3 ++- xarray/core/dataarray.py | 9 ++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 91f17cf9e0b..74287a6ff27 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -22,7 +22,8 @@ Breaking changes ~~~~~~~~~~~~~~~~ - Supplying ``coords`` as a dictionary to the ``DataArray`` constructor without - also supplying an explicit ``dims`` argument is no longer supported. (:issue:`727`). + also supplying an explicit ``dims`` argument is no longer supported. This + behavior was deprecated in version 0.9 but is now an error (:issue:`727`). By `Joe Hamman `_. Enhancements diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index c732efb71f1..3f0fa85ba10 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -52,11 +52,10 @@ def _infer_coords_and_dims(shape, coords, dims): 'dictionary like ``coords`` has been ' 'deprecated. Use an explicit list of ' '``dims`` instead.') - else: - for n, (dim, coord) in enumerate(zip(dims, coords)): - coord = as_variable(coord, - name=dims[n]).to_index_variable() - dims[n] = coord.name + 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) else: for d in dims: