pydata / pydata/xarray

DataArray.rename does not copy data

Open
#9,432 13 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

contrib-help-wanted topic-documentation topic-faq usage question
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:

  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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.