Raise when assigning attrs to virtual variables (default coordinate arrays)
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
Description
Discussed in https://github.com/pydata/xarray/discussions/8567
Originally posted by matthew-brett December 21, 2023
Sorry for the introductory question, but we (@ivanov and I) ran into this behavior while experimenting:
import numpy as np
data = np.zeros((3, 4, 5))
ds = xr.DataArray(data, dims=('i', 'j', 'k'))
print(ds['k'].attrs)
This shows {} as we might reasonably expect. But then:
ds['k'].attrs['foo'] = 'bar'
print(ds['k'].attrs)
This also gives {}, which we found surprising. We worked out why that was, after a little experimentation (the default coordinate arrays seems to get created on the fly and garbage collected immediately). But it took us a little while. Is that as intended? Is there a way of making this less confusing?
Thanks for any help.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue names no source file or test. Start by running the Python/NumPy reproducer with a default coordinate array, then trace how the virtual variable and its attrs are created and discarded. Done should be an explicit error when assigning attrs to such a variable, with a regression test covering the reported example.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- numpy, python
- Domain
- data
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100