DataArray.rename does not copy data
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
Description
What is your issue?
From the documentation: https://docs.xarray.dev/en/stable/generated/xarray.DataArray.rename.html#xarray.DataArray.rename it isn't clear if the contained data is copied (new memory), or whether it is retained.
It says:
Returns a new DataArray with renamed coordinates, dimensions or a new name.
which may indicate that it copies, however it doesn't:
import numpy as np
import xarray as xr
N = 100
O = xr.DataArray(np.random.rand(N, N), dims=["state [r]", "state [c]"])
s = xr.DataArray(np.random.rand(N, 2), dims=["state [c]", "spin"])
import time
t0 = time.time()
_ = O.rename({"state [r]": "state [c]", "state [c]": "state [r]"})
print("rename: ", time.time() - t0)
print(np.shares_memory(O.values, _.values))
t0 = time.time()
_ = O.values.copy()
print("copy: ", time.time() - t0)
print(np.shares_memory(O.values, _))
Now:
- I think the current behaviour is correct. It is nice that we can rename stuff, without having to copy the entire array.
- I only think a clarification is necessary for the documentation.
- If there are plans for this to copy it, then I think there should atleast be some
inplacearguments allowing this.
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 with the DataArray.rename documentation at the linked API page and inspect how its current return behavior is described. Clarify whether renaming retains or copies the contained data, and ensure the documentation accurately states the behavior and any relevant implications.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- numpy, python
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100