From 6b2b0dcabd075e62c5f31352d2cd31e36cc0f989 Mon Sep 17 00:00:00 2001 From: Matthieu Ancellin Date: Wed, 15 Apr 2020 17:45:01 +0200 Subject: [PATCH 1/4] Improve error message: automatic alignment during in-place operation. --- xarray/core/dataarray.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index 63cba53b689..3e6cc7bd776 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -54,7 +54,7 @@ from .formatting import format_item from .indexes import Indexes, default_indexes, propagate_indexes from .indexing import is_fancy_indexer -from .merge import PANDAS_TYPES, _extract_indexes_from_coords +from .merge import PANDAS_TYPES, _extract_indexes_from_coords, MergeError from .options import OPTIONS from .utils import Default, ReprObject, _check_inplace, _default, either_dict_or_kwargs from .variable import ( @@ -2663,8 +2663,15 @@ def func(self, other): # don't support automatic alignment with in-place arithmetic. other_coords = getattr(other, "coords", None) other_variable = getattr(other, "variable", other) - with self.coords._merge_inplace(other_coords): - f(self.variable, other_variable) + try: + with self.coords._merge_inplace(other_coords): + f(self.variable, other_variable) + except MergeError: + raise ValueError( + "Automatic alignment is not supported for in-place operations.\n" + "Consider aligning the indices manually or using a not-in-place operation.\n" + "See https://github.com/pydata/xarray/issues/3910 for more explanations." + ) return self return func From 2f30c0f4b390f64f189254ee6506d0e10373e982 Mon Sep 17 00:00:00 2001 From: Matthieu Ancellin Date: Wed, 15 Apr 2020 18:01:21 +0200 Subject: [PATCH 2/4] Sorted imports. --- xarray/core/dataarray.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index 3e6cc7bd776..e9db7855dea 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -54,7 +54,7 @@ from .formatting import format_item from .indexes import Indexes, default_indexes, propagate_indexes from .indexing import is_fancy_indexer -from .merge import PANDAS_TYPES, _extract_indexes_from_coords, MergeError +from .merge import PANDAS_TYPES, MergeError, _extract_indexes_from_coords from .options import OPTIONS from .utils import Default, ReprObject, _check_inplace, _default, either_dict_or_kwargs from .variable import ( From fd96e0feff87336d7e230a237f6e2a0d6ca06d10 Mon Sep 17 00:00:00 2001 From: Matthieu Ancellin Date: Wed, 15 Apr 2020 18:24:57 +0200 Subject: [PATCH 3/4] Fix tests. --- xarray/tests/test_dataarray.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index c3e5aafabfe..0e2268447e6 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -1921,9 +1921,9 @@ def test_inplace_math_basics(self): def test_inplace_math_automatic_alignment(self): a = DataArray(range(5), [("x", range(5))]) b = DataArray(range(1, 6), [("x", range(1, 6))]) - with pytest.raises(xr.MergeError): + with pytest.raises(ValueError): a += b - with pytest.raises(xr.MergeError): + with pytest.raises(ValueError): b += a def test_math_name(self): From a82b0232386de1ee297ce7d0fd37a154041244b0 Mon Sep 17 00:00:00 2001 From: Matthieu Ancellin Date: Fri, 17 Apr 2020 15:46:19 +0200 Subject: [PATCH 4/4] Add suggestions from S. Hoyer. --- xarray/core/dataarray.py | 6 +++--- xarray/tests/test_dataarray.py | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index e9db7855dea..0ec8b86fa39 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -2666,12 +2666,12 @@ def func(self, other): try: with self.coords._merge_inplace(other_coords): f(self.variable, other_variable) - except MergeError: - raise ValueError( + except MergeError as exc: + raise MergeError( "Automatic alignment is not supported for in-place operations.\n" "Consider aligning the indices manually or using a not-in-place operation.\n" "See https://github.com/pydata/xarray/issues/3910 for more explanations." - ) + ) from exc return self return func diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 0e2268447e6..d51fc10d85c 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -1921,9 +1921,9 @@ def test_inplace_math_basics(self): def test_inplace_math_automatic_alignment(self): a = DataArray(range(5), [("x", range(5))]) b = DataArray(range(1, 6), [("x", range(1, 6))]) - with pytest.raises(ValueError): + with pytest.raises(xr.MergeError, match="Automatic alignment is not supported"): a += b - with pytest.raises(ValueError): + with pytest.raises(xr.MergeError, match="Automatic alignment is not supported"): b += a def test_math_name(self):