pydata / pydata/xarray

clean up the API for renaming and changing dimensions / coordinates

Open
#4,825 5 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

API design
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

From #4108:

I wonder if it would be better to first "reorganize" all of the existing functions: we currently have rename (and Dataset.rename_dims / Dataset.rename_vars), set_coords, reset_coords, set_index, reset_index and swap_dims, which overlap partially. For example, the code sample from #4417 works if instead of

ds = ds.rename(b='x')
ds = ds.set_coords('x')

we use

ds = ds.set_index(x="b")

and something similar for the code sample in #4107.

I believe we currently have these use cases (not sure if that list is complete, though):

  • rename a DataArrayrename
  • rename a existing variable to a name that is not yet in the object → rename / Dataset.rename_vars / Dataset.rename_dims
  • convert a data variable to a coordinate (not a dimension coordinate) → set_coords
  • convert a coordinate (not a dimension coordinate) to a data variable → reset_coords
  • swap a existing dimension coordinate with a coordinate (which may not exist) and rename the dimension → swap_dims
  • use a existing coordinate / data variable as a dimension coordinate (do not rename the dimension) → set_index
  • stop using a coordinate as dimension coordinate and append _ to its name (do not rename the dimension) → reset_index
  • use two existing coordinates / data variables as a MultiIndex → set_index
  • stop using a MultiIndex as a dimension coordinate and use its levels as coordinates → reset_index

Sometimes, some of these can be emulated by combinations of others, for example:

# x is a dimension without coordinates
assert_identical(ds.set_index({"x": "b"}), ds.swap_dims({"x": "b"}).rename({"b": "x"}))
assert_identical(ds.swap_dims({"x": "b"}), ds.set_index({"x": "b"}).rename({"x": "b"}))

and, with this PR:

assert_identical(ds.set_index({"x": "b"}), ds.set_coords("b").rename({"b": "x"}))
assert_identical(ds.swap_dims({"x": "b"}), ds.rename({"b": "x"}))

which means that it would increase the overlap of rename, set_index, and swap_dims.

In any case I think we should add a guide which explains which method to pick in which situation (or extend howdoi).

Originally posted by @keewis in https://github.com/pydata/xarray/issues/4108#issuecomment-761907785

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 by reviewing rename, set_coords, reset_coords, set_index, reset_index, and swap_dims, along with the examples and linked issues #4108, #4417, and #4107. Done means documenting which method fits each listed use case, including overlaps and equivalent combinations, in a guide or howdoi entry.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, documentation
Issue type
Documentation
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.