Skip to content

DataArray.rename does not copy data #9432

Open
@zerothi

Description

@zerothi

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:

  1. I think the current behaviour is correct. It is nice that we can rename stuff, without having to copy the entire array.
  2. I only think a clarification is necessary for the documentation.
  3. If there are plans for this to copy it, then I think there should atleast be some inplace arguments allowing this.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions