pydata / pydata/xarray

Dropping indices doesn't work for dimensions without coordinates

Open
#3,208 6 comments 1 reaction 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

MCVE Code Sample

Calling the following:

arr = xr.DataArray(
    np.arange(10),
    dims=['x']
)

arr.sel(x=[1, 2])  # This works

arr.drop([1, 2], dim='x')  # Raises an exception

Produces this traceback:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~/anaconda3/envs/fibricheck-py36/lib/python3.6/site-packages/xarray/core/dataset.py in drop(self, labels, dim, errors)
   3135             try:
-> 3136                 index = self.indexes[dim]
   3137             except KeyError:

~/anaconda3/envs/fibricheck-py36/lib/python3.6/site-packages/xarray/core/indexes.py in __getitem__(self, key)
     32     def __getitem__(self, key):
---> 33         return self._indexes[key]
     34

KeyError: 'x'

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-1-d0f96f591c69> in <module>
      9 arr.sel(x=[1, 2])  # This works
     10
---> 11 arr.drop([1, 2], dim='x')  # Raises an exception

~/anaconda3/envs/fibricheck-py36/lib/python3.6/site-packages/xarray/core/dataarray.py in drop(self, labels, dim, errors)
   1690         if utils.is_scalar(labels):
   1691             labels = [labels]
-> 1692         ds = self._to_temp_dataset().drop(labels, dim, errors=errors)
   1693         return self._from_temp_dataset(ds)
   1694

~/anaconda3/envs/fibricheck-py36/lib/python3.6/site-packages/xarray/core/dataset.py in drop(self, labels, dim, errors)
   3137             except KeyError:
   3138                 raise ValueError(
-> 3139                     'dimension %r does not have coordinate labels' % dim)
   3140             new_index = index.drop(labels, errors=errors)
   3141             return self.loc[{dim: new_index}]

ValueError: dimension 'x' does not have coordinate labels
Expected Output

Calling drop should return the following DataArray:

<xarray.DataArray (x: 8)>
array([0, 3, 4, 5, 6, 7, 8, 9])
Dimensions without coordinates: x
Problem Description

Since I can index arr with dimension x, I would expect drop to work as well. The current behaviour seems inconsistent to me.

Output of xr.show_versions()
INSTALLED VERSIONS
------------------
commit: None
libhdf5: 1.10.2
libnetcdf: 4.6.3

xarray: 0.12.3
pandas: 0.24.2
numpy: 1.16.4
scipy: 1.1.0
netCDF4: 1.5.1.2
pydap: None
h5netcdf: None
h5py: 2.7.0
Nio: None
zarr: None
cftime: 1.0.3.4
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: 2.1.0
distributed: None
matplotlib: 3.0.3
cartopy: None
seaborn: 0.9.0
numbagg: None
setuptools: 40.8.0
pip: 19.2.1
conda: None
pytest: 4.5.0
IPython: 7.4.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

Reproduce the MCVE with a DataArray lacking coordinates, then start at DataArray.drop and the Dataset.drop path shown in the traceback. Verify that dropping [1, 2] along x returns the expected eight-element array while preserving the dimension without coordinates.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.