disallow boolean coordinates?
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
Description
Today I stumbled over a small pitfall, which I think could be avoided:
I am working with arrays that have axes labeled with categorical values and I ended up using True/False as labels for some binary categories:
test = xarray.DataArray(
numpy.ones((3,2)),
dims=["binary","ternary"],
coords={"ternary":[3,7,9],"binary":[False,True]}
)
now came the big surprise, when I wanted to reduce over selections of the data:
test.sel(ternary=[9,3,7]) # does exactly what I expect and gives me the correctly permuted 3x2 array
test.sel(binary=[True,False]) # does not do what I expect
Instead of using the coordinate values like with the ternary category, it uses the list as boolean mask and hence I get a 3x1 array at the binary=False coordinate.
I assume that this behavior is reasonable in most cases - And I for sure will stop using bools as binary category labels.
That said in the above case the conceptually identical call results in completely different outcome.
My (radical) proposal would be: forbid binary coordinates in general to avoid such confusion.
Curious about your thoughts! Hth,
Marti
Originally posted by @martinitus in https://github.com/pydata/xarray/discussions/4861
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
Start by examining DataArray.sel and how list-like boolean indexers are interpreted for coordinates. Compare the behavior of boolean and numeric coordinate selections, then determine the intended API change before implementing it. Done means the project has a decided, tested behavior for boolean coordinates that avoids the reported ambiguity.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- numpy, python
- Domain
- data
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100