Minimize `.item()` call
Open
Nobody has claimed this yet.
topic-indexing
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
Description
MCVE Code Sample
I want to minimize the number of calls .item() within my data analysis.
It often happens
- when putting a 0d-DataArray into a slice
da = xr.DataArray([0.5, 4.5, 2.5], dims=['x'], coords={'x': [0, 1, 2]})
da[: da.argmax()]
->
TypeError: 'DataArray' object cannot be interpreted as an integer
- when using a 0d-DataArray for selecting
da = xr.DataArray([0.5, 4.5, 2.5], dims=['x'], coords={'x': [0, 0, 2]})
da.sel(x=da['x'][0])
->
IndexError: arrays used as indices must be of integer (or boolean) type
Both cases, I need to call '.item()'.
It is not a big issue, but I think it would be nice if xarray becomes more self-contained.
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
Reproduce the two MCVE examples involving DataArray slicing and .sel(), then trace how these operations handle 0d-DataArray values. Done means both examples work without an explicit .item() call, with regression tests covering the reported behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- data
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100