html_url,issue_url,id,node_id,user,created_at,updated_at,author_association,body,reactions,performed_via_github_app,issue https://github.com/pydata/xarray/issues/2835#issuecomment-1256553736,https://api.github.com/repos/pydata/xarray/issues/2835,1256553736,IC_kwDOAMm_X85K5X0I,4447466,2022-09-23T18:47:58Z,2022-09-27T15:00:51Z,CONTRIBUTOR,"Just for the record, I just ran into this for the specific case of *nested* dictionary attrs in Dataarray.attrs. It's definitely an issue in 2022.3.0 and 2022.6.0. Here's a minimal test example in case anyone else runs into this too... ```python # MINIMAL EXAMPLE import xarray as xr import numpy as np data = xr.DataArray(np.random.randn(2, 3), dims=(""x"", ""y""), coords={""x"": [10, 20]}) data.attrs['flat']='0' data.attrs['nested']={'level1':'1'} data2 = data.copy(deep=True) data2.attrs['flat']='2' # OK # data2.attrs['nested']={'level1':'2'} # OK # data2.attrs['nested']['level1'] = '2' # Fails - overwrites data data2.attrs['nested'].update({'level1':'2'}) # Fails - overwrites data print(data.attrs) print(data2.attrs) ``` Outputs In XR 2022.3.0 and 2022.6.0 this gives (incorrect): ``` {'flat': '0', 'nested': {'level1': '2'}} {'flat': '2', 'nested': {'level1': '2'}} ``` As a work-around, safe attrs copy with deepcopy works: ```python data2 = data.copy(deep=True) data2.attrs = copy.deepcopy(data.attrs) ``` With correct results after modification: ``` {'flat': '0', 'nested': {'level1': '1'}} {'flat': '2', 'nested': {'level1': '2'}} ``` EDIT 26th Sept: retested in 2022.6.0 and found it was, in fact, failing there too. Updated comment to reflect this.","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,423742774 https://github.com/pydata/xarray/issues/2835#issuecomment-1258611190,https://api.github.com/repos/pydata/xarray/issues/2835,1258611190,IC_kwDOAMm_X85LBOH2,4447466,2022-09-26T20:43:01Z,2022-09-26T20:43:01Z,CONTRIBUTOR,"> Ok, I thought that copying attrs was fixed. Seems like it did not... Sorry for the mix-up there - think I initially tested in 2022.6 with the extra `data2.attrs = copy.deepcopy(data.attrs)` implemented. Caught it with the new test routine 😄 ","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,423742774 https://github.com/pydata/xarray/issues/2835#issuecomment-1258538050,https://api.github.com/repos/pydata/xarray/issues/2835,1258538050,IC_kwDOAMm_X85LA8RC,4447466,2022-09-26T19:48:39Z,2022-09-26T19:52:14Z,CONTRIBUTOR,"OK, new test now pushed as #7086. (Hopefully added in the right place and style!) A couple of additional notes: - Revision to my comment above: this actually fails in 2022.3 *and* 2022.6 for nested attribs. - I took a look at the source code in `dataarray.py`, but couldn't see an obvious way to fix this and/or didn't understand the attrs copying process generally. - I tested the equivalent case for DataSet attrs too (see below), and this seems fine as per your previous comments above, so I think https://github.com/pydata/xarray/pull/2839 (which includes a ds level test) still applies to `ds.attrs`, however the issue *does* affect the individual arrays within the dataset still (as expected). ```python import xarray as xr ds = xr.Dataset({""a"": ([""x""], [1, 2, 3])}, attrs={""t"": 1, ""nested"":{""t2"": 1}}) ds.a.attrs = {""t"": 'a1', ""nested"":{""t2"": 'a1'}} ds2 = ds.copy(deep=True) ds.attrs[""t""] = 5 ds.attrs[""nested""][""t2""] = 10 ds2.a.attrs[""t""] = 'a2' ds2.a.attrs[""nested""][""t2""] = 'a2' print(ds.attrs) print(ds.a.attrs) print(ds2.attrs) print(ds2.a.attrs) ``` Results in: ``` {'t': 5, 'nested': {'t2': 10}} {'t': 'a1', 'nested': {'t2': 'a2'}} {'t': 1, 'nested': {'t2': 1}} {'t': 'a2', 'nested': {'t2': 'a2'}} ```","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,423742774 https://github.com/pydata/xarray/issues/2835#issuecomment-1258231700,https://api.github.com/repos/pydata/xarray/issues/2835,1258231700,IC_kwDOAMm_X85K_xeU,4447466,2022-09-26T15:38:18Z,2022-09-26T15:38:18Z,CONTRIBUTOR,"Absolutely @headtr1ck, glad that it was useful - I'm a bit green re: tests and PRs to large projects, but will make a stab at it. I'm just consulting the Contributing guide now.","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,423742774