pydata / pydata/xarray

Error when supplying a tuple of dimensions to DataArray.sortby()

Open
#4,821 18 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug contrib-good-first-issue
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

What happened:

A KeyError is thrown when supplying a tuple of dimensions to DataArray.sortby()

What you expected to happen:

The dataarray to be sorted according to the dimensions.

Minimal Complete Verifiable Example:

import xarray as xr
import numpy as np

da=xr.DataArray(np.random.rand(3,3), coords=(('x', range(3, 0, -1)), ('y', range(3, 0, -1))))
da.sortby(da.dims)

Anything else we need to know?:

If the tuple is cast to a list it works correctly:

da.sortby(list(da.dims))

Environment:

Output of xr.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.12 (default, Nov 11 2020, 22:22:08)
[GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.46.4)]
python-bits: 64
OS: Darwin
OS-release: 18.7.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: None
LOCALE: en_US.UTF-8
libhdf5: None
libnetcdf: None

xarray: 0.16.2
pandas: 1.1.4
numpy: 1.19.4
scipy: 1.5.4
netCDF4: None
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: None
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: 1.3.2
dask: 2020.12.0
distributed: None
matplotlib: 3.3.3
cartopy: None
seaborn: None
numbagg: None
pint: None
setuptools: 50.3.2
pip: 20.3.3
conda: None
pytest: 6.2.1
IPython: 7.16.1
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 minimal example against DataArray.sortby(), comparing da.sortby(da.dims) with da.sortby(list(da.dims)). Trace the DataArray.sortby() entry point and add coverage showing that the tuple form sorts according to the dimensions without raising KeyError.

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
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.