Skip to content

Clearer Vectorized Indexing example #4433

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 3 commits into from
Sep 18, 2020
Merged

Conversation

max-sixty
Copy link
Collaborator

  • Passes isort . && black . && mypy . && flake8
  • User visible changes (including notable bug fixes) are documented in whats-new.rst

I found the existing example not that clear as to why it was selecting [[1,1],[5,5]], I think this is more obvious (open to disagreement of course!)

Before

In [37]: da = xr.DataArray(
   ....:     np.arange(12).reshape((3, 4)),
   ....:     dims=["x", "y"],
   ....:     coords={"x": [0, 1, 2], "y": ["a", "b", "c", "d"]},
   ....: )
   ....: 

In [38]: da
Out[38]: 
<xarray.DataArray (x: 3, y: 4)>
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
Coordinates:
  * x        (x) int64 0 1 2
  * y        (y) <U1 'a' 'b' 'c' 'd'

In [39]: da[[0, 1], [1, 1]]
Out[39]: 
<xarray.DataArray (x: 2, y: 2)>
array([[1, 1],
       [5, 5]])
Coordinates:
  * x        (x) int64 0 1
  * y        (y) <U1 'b' 'b'

After:

In [4]:     da[[1, 2, 2], [0, 3]]
Out[4]:
<xarray.DataArray (x: 3, y: 2)>
array([[ 4,  7],
       [ 8, 11],
       [ 8, 11]])
Coordinates:
  * x        (x) int64 1 2 2
  * y        (y) <U1 'a' 'd'`

@alexamici
Copy link
Collaborator

Yes, please.

Minor: also I'd personally find even more intuitive the selection da[[0, 2, 2], [1, 3]] that still shows you can repeat indexes, but somewhat looks more like a downsampling. Your call.

@dcherian dcherian merged commit fd3eb21 into pydata:master Sep 18, 2020
@dcherian
Copy link
Contributor

Thanks @max-sixty

@dcherian
Copy link
Contributor

& @alexamici for suggestions!

@max-sixty max-sixty deleted the vec-example branch September 18, 2020 18:36
dcherian added a commit to dcherian/xarray that referenced this pull request Oct 9, 2020
…pagate-attrs

* 'propagate-attrs' of github.com:dcherian/xarray: (22 commits)
  silence sphinx warnings about broken rst (pydata#4448)
  Xarray open_mfdataset with engine Zarr (pydata#4187)
  Fix release notes formatting (pydata#4443)
  fix typo in io.rst (pydata#4250)
  Fix typo (pydata#4181)
  Fix release notes typo
  New whatsnew section
  Add notes re doctests (pydata#4440)
  Fixed dask.optimize on datasets (pydata#4438)
  Release notes for 0.16.1 (pydata#4435)
  Small updates to How-to-release + lint (pydata#4436)
  Fix doctests (pydata#4439)
  add a ci for doctests (pydata#4437)
  preserve original dimension, coordinate and variable order in ``concat`` (pydata#4419)
  Fix for h5py deepcopy issues (pydata#4426)
  Keep the original ordering of the coordinates (pydata#4409)
  Clearer Vectorized Indexing example (pydata#4433)
  Revert "Fix optimize for chunked DataArray (pydata#4432)" (pydata#4434)
  Fix optimize for chunked DataArray (pydata#4432)
  fix doc dataarray to netcdf (pydata#4424)
  ...
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants