Skip to content

Attributes are dropped after clip even if keep_attrs is True #3433

@mraspaud

Description

@mraspaud

MCVE Code Sample

import xarray as xr
import numpy as np

arr = xr.DataArray(np.ones((5, 5)), attrs={'units': 'K'})
xr.set_options(keep_attrs=True)
arr
# <xarray.DataArray (dim_0: 5, dim_1: 5)>
# array([[1., 1., 1., 1., 1.],
#        [1., 1., 1., 1., 1.],
#        [1., 1., 1., 1., 1.],
#        [1., 1., 1., 1., 1.],
#        [1., 1., 1., 1., 1.]])
# Dimensions without coordinates: dim_0, dim_1
# Attributes:
#     units:    K

arr.clip(0, 1)
# <xarray.DataArray (dim_0: 5, dim_1: 5)>
# array([[1., 1., 1., 1., 1.],
#        [1., 1., 1., 1., 1.],
#        [1., 1., 1., 1., 1.],
#        [1., 1., 1., 1., 1.],
#        [1., 1., 1., 1., 1.]])
# Dimensions without coordinates: dim_0, dim_1

Expected Output

I would expect the attributes to be kept

Problem Description

keep_attrs set to True doesn't seem to be respected with the DataArray.clip method.

Output of xr.show_versions()

Details INSTALLED VERSIONS ------------------ commit: None python: 3.7.3 | packaged by conda-forge | (default, Jul 1 2019, 21:52:21) [GCC 7.3.0] python-bits: 64 OS: Linux OS-release: 3.10.0-1062.1.1.el7.x86_64 machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_GB.UTF-8 LOCALE: en_GB.UTF-8 libhdf5: 1.10.5 libnetcdf: 4.6.2

xarray: 0.14.0
pandas: 0.25.1
numpy: 1.17.0
scipy: 1.3.0
netCDF4: 1.5.1.2
pydap: None
h5netcdf: 0.7.4
h5py: 2.10.0
Nio: None
zarr: 2.3.2
cftime: 1.0.3.4
nc_time_axis: None
PseudoNetCDF: None
rasterio: 1.0.28
cfgrib: None
iris: None
bottleneck: None
dask: 2.6.0
distributed: 2.6.0
matplotlib: 3.1.1
cartopy: 0.17.0
seaborn: None
numbagg: None
setuptools: 41.4.0
pip: 19.3
conda: None
pytest: 5.0.1
IPython: 7.8.0
sphinx: 2.2.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic-metadataRelating to the handling of metadata (i.e. attrs and encoding)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions