pydata / pydata/xarray

Assignment

Open
#1,519 6 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

topic-documentation topic-error reporting topic-indexing usage question
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

Assignment of individual elements / subsets seems to be only partially supported, but I have not found clear documentation on it. After playing a bit around with xarray I believe the current support boils down to the following (I list some issues I see with the current documentation and how things seem to work).

DataSet

  • Change a element in a associated DataArray
    • ds.loc[selector_dict].NAME_OF_VARIABLE = -1 Doesn't work but at least throws an error (Use __setitem__ style assignment (e.g., ds['name'] = ...) instead to assign variables.)
    • ds.loc[selector_dict]["NAME_OF_VARIABLE"] = -1 Silently fails
      • Is it possible to throw an exception as well? I believe this usage variant is "more common", as NAME_OF_VARIABLE may only be known at runtime. #974 May fix this (?).
    • Same goes for ds.sel

DataArray
(Can be obtained via ds['name'])

  • Using .sel (which is documented to return a new DataArray, so arguably it's expected not to work)
    • ds["NAME_OF_VARIABLE"].sel(**selector_dict) = -1 Invalid python syntax
    • ds["NAME_OF_VARIABLE"].sel(**selector_dict).values = -1 Silently fails
  • Using .loc
    • ds["NAME_OF_VARIABLE"].loc[selector_dict] = -1 works

Is this the recommended behaviour? Then I believe the wrong pattern of ds.loc[selector_dict]["NAME_OF_VARIABLE"] = -1 should be documented and ds[NAME_OF_VARIABLE].loc[selector_dict] = -1 instead suggested.

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

Start by comparing the assignment behavior described for Dataset.loc, Dataset.sel, DataArray.sel, and DataArray.loc, then check the existing xarray documentation for these entry points. Done means the recommended assignment pattern and the behavior of silent failures are clearly documented, or the requested exception behavior is explicitly resolved.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data, documentation
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.