Description
What happened?
In previous versions of xarray, testing numerical equivalence of two DataArrays was possible using the built-in operator '==' and without side affects. Now in version 2022.6.0, when one DataArray lacks an attribute that the other DataArray has, the DataArray with the attribute is mutilated during comparison leading to an empty attrs dictionary.
What did you expect to happen?
DataArray_1 == DataArray_2 should not have side affects.
Minimal Complete Verifiable Example
import xarray as xr
da_withunits = xr.DataArray([1, 1, 1], coords={"frequency": [1, 2, 3]})
da_withunits.frequency.attrs["units"] = "GHz"
print(da_withunits.frequency.units)
da_withoutunits = xr.DataArray([1, 1, 1], coords={"frequency": [1, 2, 3]})
print(da_withunits == da_withoutunits)
print(da_withunits.frequency.units)
MVCE confirmation
- Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
- Complete example — the example is self-contained, including all data and the text of any traceback.
- Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
- New issue — a search of GitHub Issues suggests this is not a duplicate.
Relevant log output
GHz
<xarray.DataArray (frequency: 3)>
array([ True, True, True])
Coordinates:
* frequency (frequency) int32 1 2 3
Traceback (most recent call last):
File "d:\projects\ssdv\mvce.py", line 9, in <module>
print(da_withunits.frequency.units)
File "...\AppData\Local\Programs\Python\Python39\lib\site-packages\xarray\core\common.py", line 256, in __getattr__
raise AttributeError(
AttributeError: 'DataArray' object has no attribute 'units'
Anything else we need to know?
No response
Environment
xarray: 2022.6.0
pandas: 1.4.3
numpy: 1.23.1
scipy: 1.9.0
netCDF4: None
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: None
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: 3.5.2
cartopy: None
seaborn: None
numbagg: None
fsspec: None
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 63.2.0
pip: 22.2.1
conda: None
pytest: 7.1.2
IPython: 8.4.0
sphinx: None