From 71bc14dbc910b59a321ab06eb76f53be579f994f Mon Sep 17 00:00:00 2001 From: Yohai Bar Sinai <6164157+yohai@users.noreply.github.com> Date: Wed, 6 Feb 2019 21:46:46 -0500 Subject: [PATCH 1/5] fix renaming --- xarray/core/common.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/xarray/core/common.py b/xarray/core/common.py index 30ea56f3496..d981034e3d3 100644 --- a/xarray/core/common.py +++ b/xarray/core/common.py @@ -856,7 +856,11 @@ def where(self, cond, other=dtypes.NA, drop=False): self = self.isel(**indexers) cond = cond.isel(**indexers) - return ops.where_method(self, cond, other) + result = ops.where_method(self, cond, other) + if isinstance(self, DataArray): + result.name = self.name + + return result def close(self): """Close any files linked to this object From afbdcb5489a6d48a3520e675c7c58a4891ff3aab Mon Sep 17 00:00:00 2001 From: Yohai Bar Sinai <6164157+yohai@users.noreply.github.com> Date: Wed, 6 Feb 2019 22:00:50 -0500 Subject: [PATCH 2/5] formatting --- xarray/core/common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/common.py b/xarray/core/common.py index d981034e3d3..b08aeb9c043 100644 --- a/xarray/core/common.py +++ b/xarray/core/common.py @@ -859,7 +859,7 @@ def where(self, cond, other=dtypes.NA, drop=False): result = ops.where_method(self, cond, other) if isinstance(self, DataArray): result.name = self.name - + return result def close(self): From 27ed788126a21e41da5e6079ba54e3463f3b02fe Mon Sep 17 00:00:00 2001 From: Yohai Bar Sinai <6164157+yohai@users.noreply.github.com> Date: Wed, 6 Feb 2019 22:24:29 -0500 Subject: [PATCH 3/5] added tests --- xarray/tests/test_dataarray.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 20872aa4088..09c0f003888 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -3694,6 +3694,15 @@ def test_raise_no_warning_for_nan_in_binary_ops(): assert len(record) == 0 +def test_name_in_masking(): + name = 'RingoStarr' + da = xr.DataArray(range(10), coords=[('x', range(10))], name=name) + assert da.where(da > 5).name == name + assert da.where((da > 5).rename('YokoOno')).name == name + assert da.where(da > 5, drop=True).name == name + assert da.where((da > 5).rename('YokoOno'), drop=True).name == name + + class TestIrisConversion(object): @requires_iris def test_to_and_from_iris(self): From fd1c6a32efed2fe0b0956ec794f2b189c3526d11 Mon Sep 17 00:00:00 2001 From: Yohai Bar Sinai <6164157+yohai@users.noreply.github.com> Date: Sat, 9 Feb 2019 15:41:15 -0500 Subject: [PATCH 4/5] shoyer's solution --- xarray/core/common.py | 6 +----- xarray/core/computation.py | 9 +++++++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/xarray/core/common.py b/xarray/core/common.py index b08aeb9c043..30ea56f3496 100644 --- a/xarray/core/common.py +++ b/xarray/core/common.py @@ -856,11 +856,7 @@ def where(self, cond, other=dtypes.NA, drop=False): self = self.isel(**indexers) cond = cond.isel(**indexers) - result = ops.where_method(self, cond, other) - if isinstance(self, DataArray): - result.name = self.name - - return result + return ops.where_method(self, cond, other) def close(self): """Close any files linked to this object diff --git a/xarray/core/computation.py b/xarray/core/computation.py index b9303a5681d..86a2f05b041 100644 --- a/xarray/core/computation.py +++ b/xarray/core/computation.py @@ -199,6 +199,7 @@ def apply_dataarray_ufunc(func, *args, **kwargs): signature = kwargs.pop('signature') join = kwargs.pop('join', 'inner') exclude_dims = kwargs.pop('exclude_dims', _DEFAULT_FROZEN_SET) + keep_attrs = kwargs.pop('keep_attrs', True) if kwargs: raise TypeError('apply_dataarray_ufunc() got unexpected keyword ' 'arguments: %s' % list(kwargs)) @@ -207,7 +208,10 @@ def apply_dataarray_ufunc(func, *args, **kwargs): args = deep_align(args, join=join, copy=False, exclude=exclude_dims, raise_on_invalid=False) - name = result_name(args) + if keep_attrs and hasattr(args[0], 'name'): + name = args[0].name + else: + name = result_name(args) result_coords = build_output_coords(args, signature, exclude_dims) data_vars = [getattr(a, 'variable', a) for a in args] @@ -986,7 +990,8 @@ def earth_mover_distance(first_samples, return apply_dataarray_ufunc(variables_ufunc, *args, signature=signature, join=join, - exclude_dims=exclude_dims) + exclude_dims=exclude_dims, + keep_attrs=keep_attrs) elif any(isinstance(a, Variable) for a in args): return variables_ufunc(*args) else: From 3a5967650559920518562c32652e76c5fcc2c21f Mon Sep 17 00:00:00 2001 From: Yohai Bar Sinai <6164157+yohai@users.noreply.github.com> Date: Mon, 11 Feb 2019 09:15:55 -0500 Subject: [PATCH 5/5] what's new --- doc/whats-new.rst | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index fb738f04c6d..e76fe129f96 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -24,7 +24,7 @@ Breaking changes - Remove support for Python 2. This is the first version of xarray that is Python 3 only. (:issue:`1876`). By `Joe Hamman `_. -- The `compat` argument to `Dataset` and the `encoding` argument to +- The `compat` argument to `Dataset` and the `encoding` argument to `DataArray` are deprecated and will be removed in a future release. (:issue:`1188`) By `Maximilian Roos `_. @@ -82,7 +82,9 @@ Bug fixes - Fix ``open_rasterio`` creating a WKT CRS instead of PROJ.4 with ``rasterio`` 1.0.14+ (:issue:`2715`). By `David Hoese `_. .. _whats-new.0.11.3: v0.11.3 (26 January 2019)