swap_coords() function ?
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
Description
Hi all,
I have a DataArray with coordinates 'time'. I want to swap the time coordinate with another coordinate which is not in the array.
swap_dims() allows to give a non-existing dimension but I cannot define the coords at the same time. I usually use a workaround as follow.
a = xr.DataArray(np.random.rand(10), dims='time', coords={'time':1.e-4*np.arange(10)})
celerity=3e8
space = celerity * a['time']
a = a.swap_dims({'time':'space'})
a = a.assign_coords({'space':space.data})
I think it could be interesting to be able to swap_coords which could do the last two lines at the same time. Basically a swap_coords() function
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 reading the existing swap_dims() and assign_coords() behavior described in the issue, then trace how coordinate and dimension updates are tested in xarray. Define the expected swap_coords() behavior for replacing a dimension with a newly computed coordinate, and verify that the two-line workaround can be expressed as one operation.
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
- 45/100