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-1258593420,https://api.github.com/repos/pydata/xarray/issues/2835,1258593420,IC_kwDOAMm_X85LBJyM,43316012,2022-09-26T20:28:28Z,2022-09-26T20:28:28Z,COLLABORATOR,"Ok, I thought that copying attrs was fixed. Seems like it did not...
I will create a PR to fix this first, then we can use your test :)","{""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-1258235669,https://api.github.com/repos/pydata/xarray/issues/2835,1258235669,IC_kwDOAMm_X85K_ycV,43316012,2022-09-26T15:41:22Z,2022-09-26T15:41:22Z,COLLABORATOR,"> 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.
Great! Just ask if you need help :)
Basically: fork repo, make new branch on your forked repo, add test, create PR (when you push GitHub should show a popup asking if you want to create a PR anyway).","{""total_count"": 1, ""+1"": 1, ""-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
https://github.com/pydata/xarray/issues/2835#issuecomment-1256569748,https://api.github.com/repos/pydata/xarray/issues/2835,1256569748,IC_kwDOAMm_X85K5buU,43316012,2022-09-23T18:59:33Z,2022-09-23T18:59:33Z,COLLABORATOR,"I see, so it was a problem of recursively copying the attrs.
@phockett would you be open to add your example as a test in a PR such that we can avoid this in the future?","{""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-1236379727,https://api.github.com/repos/pydata/xarray/issues/2835,1236379727,IC_kwDOAMm_X85JsahP,14808389,2022-09-04T17:01:05Z,2022-09-04T17:01:05Z,MEMBER,"even with
```python
In [1]: import xarray as xr
...:
...: ds = xr.Dataset({""a"": (""x"", [1, 2, 3], {""t"": 0})}, attrs={""t"": 1})
...: ds2 = ds.copy(deep=True)
...: ds.attrs[""t""] = 5
...: ds.a.attrs[""t""] = 6
...:
...: display(ds2, ds2.a)
Dimensions: (x: 3)
Dimensions without coordinates: x
Data variables:
a (x) int64 1 2 3
Attributes:
t: 1
array([1, 2, 3])
Dimensions without coordinates: x
Attributes:
t: 0
```
I cannot reproduce. @Ch-Meier, @DerPlankton13, can either of you post a minimal example demonstrating the issue? ","{""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-1236356402,https://api.github.com/repos/pydata/xarray/issues/2835,1236356402,IC_kwDOAMm_X85JsU0y,43316012,2022-09-04T14:41:39Z,2022-09-04T14:41:39Z,COLLABORATOR,"Cannot reproduce on current master using
```python
import xarray as xr
ds = xr.Dataset({""a"": ([""x""], [1, 2, 3])}, attrs={""t"": 1})
ds2 = ds.copy(deep=True)
ds.attrs[""t""] = 5
print(ds2.attrs) # returns: {'t': 1}
```","{""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-1194130964,https://api.github.com/repos/pydata/xarray/issues/2835,1194130964,IC_kwDOAMm_X85HLP4U,18261501,2022-07-25T14:33:32Z,2022-07-25T14:33:54Z,NONE,"> Jumping from 2022.3.0 to 2022.6.0 this issue has re-emerged for me.
I am seeing the same issue as well.","{""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-1194129373,https://api.github.com/repos/pydata/xarray/issues/2835,1194129373,IC_kwDOAMm_X85HLPfd,69301768,2022-07-25T14:32:18Z,2022-07-25T14:32:18Z,NONE,Jumping from 2022.3.0 to 2022.6.0 this issue has re-emerged for me.,"{""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-475263182,https://api.github.com/repos/pydata/xarray/issues/2835,475263182,MDEyOklzc3VlQ29tbWVudDQ3NTI2MzE4Mg==,2448579,2019-03-21T14:56:03Z,2019-03-21T14:56:03Z,MEMBER,"Thanks @kefirbandi, can you send in a PR?","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,423742774