diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 1b9ba5707e8..1ccddfb2cd6 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -91,7 +91,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) diff --git a/xarray/core/computation.py b/xarray/core/computation.py index 811b216ee79..633e695694b 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] @@ -985,7 +989,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: 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):