pydata / pydata/xarray

single coordinate is overwritten with dimension by set_index

Open
#2,537 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Code Sample
import xarray as xr
d = xr.DataArray([0], coords={'coord': ('dim', [0])}, dims=['dim'])
d.set_index(append=True, inplace=True, dim=['coord'])
d.sel(dim=0)  # works
d.sel(coord=0)  # doesn't work, coord does not exist anymore
print(d)
<xarray.DataArray (dim: 1)>
array([0])
Coordinates:
  * dim      (dim) int64 0
Problem description

when a DataArray is initialized with a dimension containing only one coordinate, selection on the coordinate is not directly possible. As a workaround, we can set_index but if there is only one coordinate on a dimension, the coordinate vanishes and its values are attached to the dimension directly.

The DataArrays in my use case are generic, in some cases there are multiple coordinates and sometimes there is only one. If the one consistent coordinate is discarded for some cases, follow-up code becomes tedious.
Having a single-coordinate MultiIndex would be much more intuitive so that one can still .sel over the coordinate.

Expected Output
<xarray.DataArray (dim: 1)>
array([0])
Coordinates:
  * dim      MultiIndex
    coord   (dim) int64 0

For more than one coordinate on the dimension, the dimension becomes a MultiIndex with all the coordinates. With only a single coordinate however, this does not happen.

Output of xr.show_versions()
INSTALLED VERSIONS ------------------ commit: None python: 3.6.6.final.0 python-bits: 64 OS: Linux OS-release: 4.4.0-137-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: en_US.UTF-8 xarray: 0.10.8 pandas: 0.23.4 numpy: 1.15.1 scipy: 1.1.0 netCDF4: 1.4.1 h5netcdf: None h5py: 2.8.0 Nio: None zarr: None bottleneck: None cyordereddict: None dask: 0.19.1 distributed: None matplotlib: 2.2.3 cartopy: None seaborn: 0.9.0 setuptools: 39.1.0 pip: 10.0.1 conda: None pytest: 3.8.0 IPython: 6.5.0 sphinx: None

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 running the supplied DataArray.set_index and .sel reproduction, then inspect the set_index path and its handling of pandas MultiIndex values. Done means a single coordinate remains available as a MultiIndex level, the displayed structure matches the expected output, and selection by coord succeeds.

Written by the indexing model from the issue text.

Assessment

Tech stack
pandas, python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.