id,node_id,number,title,user,state,locked,assignee,milestone,comments,created_at,updated_at,closed_at,author_association,active_lock_reason,draft,pull_request,body,reactions,performed_via_github_app,state_reason,repo,type 2136832627,I_kwDOAMm_X85_XXpz,8755,to_zarr removes global attributes in destination dataset,8998112,open,0,,,12,2024-02-15T15:32:10Z,2024-04-29T19:22:41Z,,NONE,,,,"### What happened? Adding new variables to a zarr dataset with to_zarr() always removes the existing global attributes. New global attributes in the source dataset are not always added to the destination dataset depending on how to_zarr() is called. ### What did you expect to happen? I would expect that existing global attributes would always be preserved. If there are new global attributes I would expect them to be added to the existing global attributes instead of replacing all existing global attributes. ### Minimal Complete Verifiable Example ```Python import xarray as xr from pyproj import CRS local_zarr = 'sample.zarr' ds_sample = xr.tutorial.load_dataset(""air_temperature"") # Make a local copy ds_sample.to_zarr(local_zarr, mode='w') ds_sample = xr.open_dataset(local_zarr, engine='zarr', backend_kwargs={'consolidated':True}, chunks={}, decode_coords=True) # Create CRS metadata crs_meta = CRS.from_epsg(4326).to_cf() ds_new = xr.Dataset(data_vars={""crs"": ([], 1, crs_meta)}) ds_new.attrs['note'] = 'please add this' # Add all variables from ds_new to the zarr # NOTE: This adds the new global attribute but also removes # all existing global attributes ds_new.to_zarr(local_zarr, mode='a') # Add selected variable(s) to zarr dataset # NOTE: This does not copy new global attributes # and removes all existing global attributes # ds_new['crs'].to_zarr(local_zarr, mode='a') # Re-open local zarr store ds_sample = xr.open_dataset(local_zarr, engine='zarr', backend_kwargs={'consolidated':True}, chunks={}, decode_coords=True) ds_sample ``` ### MVCE confirmation - [X] Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray. - [X] Complete example — the example is self-contained, including all data and the text of any traceback. - [X] Verifiable example — the example copy & pastes into an IPython prompt or [Binder notebook](https://mybinder.org/v2/gh/pydata/xarray/main?urlpath=lab/tree/doc/examples/blank_template.ipynb), returning the result. - [X] New issue — a search of GitHub Issues suggests this is not a duplicate. - [X] Recent environment — the issue occurs with the latest version of xarray and its dependencies. ### Relevant log output _No response_ ### Anything else we need to know? _No response_ ### Environment
INSTALLED VERSIONS ------------------ commit: None python: 3.11.0 | packaged by conda-forge | (main, Jan 14 2023, 12:26:40) [Clang 14.0.6 ] python-bits: 64 OS: Darwin OS-release: 22.6.0 machine: arm64 processor: arm byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: ('en_US', 'UTF-8') libhdf5: 1.12.2 libnetcdf: 4.8.1 xarray: 2024.1.1 pandas: 2.2.0 numpy: 1.26.4 scipy: 1.12.0 netCDF4: 1.6.0 pydap: installed h5netcdf: 1.3.0 h5py: 3.8.0 Nio: None zarr: 2.17.0 cftime: 1.6.3 nc_time_axis: None iris: None bottleneck: 1.3.7 dask: 2024.2.0 distributed: 2024.2.0 matplotlib: 3.8.2 cartopy: 0.22.0 seaborn: None numbagg: None fsspec: 2023.12.2 cupy: None pint: 0.23 sparse: None flox: None numpy_groupies: None setuptools: 69.0.3 pip: 24.0 conda: None pytest: 8.0.0 mypy: None IPython: 8.21.0 sphinx: None
","{""url"": ""https://api.github.com/repos/pydata/xarray/issues/8755/reactions"", ""total_count"": 1, ""+1"": 1, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,,13221727,issue