pydata / pydata/xarray

nonzero method for xr.DataArray

Open
#1,772 5 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

np.nonzero to DataArray returns a wrong result,

In [4]: da = xr.DataArray(np.arange(12).reshape(4, 3), dims=['x', 'y'],
   ...:                   coords={'x': [0, 1, 2, 3], 'y': ['a', 'b', 'c']})
   ...: np.nonzero(da)
   ...: 
Out[4]: 
<xarray.DataArray (x: 2, y: 11)>
array([[0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3],
       [1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]])
Coordinates:
  * x        (x) int64 0 1 2 3
  * y        (y) <U1 'a' 'b' 'c'
Problem description

Apparently, the dimensions and the coordinates conflict each other.
I think we can have our own nonzero method, which may return a Dataset consisting of indexes and appropriate coordinates.

Output of xr.show_versions()
# Paste the output here xr.show_versions() here INSTALLED VERSIONS ------------------ commit: None python: 3.5.2.final.0 python-bits: 64 OS: Linux OS-release: 4.4.0-101-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: en_US.UTF-8

xarray: 0.9.6-172-gc58d142
pandas: 0.21.0
numpy: 1.13.1
scipy: 0.19.1
netCDF4: None
h5netcdf: None
Nio: None
bottleneck: 1.2.1
cyordereddict: None
dask: 0.16.0
matplotlib: 2.0.2
cartopy: None
seaborn: 0.7.1
setuptools: 36.5.0
pip: 9.0.1
conda: 4.3.30
pytest: 3.2.3
IPython: 6.0.0
sphinx: 1.6.3

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 reported result with the DataArray example and inspect how NumPy's nonzero dispatch interacts with xarray dimensions and coordinates. The issue names no implementation file or test; done would require an agreed nonzero behavior that preserves appropriate indexes and coordinates, plus regression coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
data
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.