Open
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
inplace
arguments allowing this.