pydata / pydata/xarray

Raise when assigning attrs to virtual variables (default coordinate arrays)

Open
#8,568 0 comments 1 reaction 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.