Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Get 0d slices of ndarrays directly from indexing #2625

Merged
merged 3 commits into from
Dec 22, 2018

Conversation

danielwe
Copy link
Contributor

@danielwe danielwe commented Dec 21, 2018

@pep8speaks
Copy link

pep8speaks commented Dec 21, 2018

Hello @danielwe! Thanks for updating the PR.

Cheers ! There are no PEP8 issues in this Pull Request. 🍻

Comment last updated on December 22, 2018 at 11:56 Hours UTC

Copy link
Member

@fujiisoup fujiisoup left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @danielwe

One minor comment but it looks really nice :)

assert (
variable[0]._data.__array_interface__["data"][0]
== variable._data.__array_interface__["data"][0]
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add more explicit tests that other developers easily find what we are testing?
e.g.

a = variable[0]
a = 3
assert a == variable[0]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might also be easier just to write a separate dedicated method for this, rather than squeezing it into the existing _assertIndexedLikeNDArray.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, my thinking about tests was a little muddled. The new solution should be better.

# We're not interested in obtaining a scalar, but rather a 0d slice.
# This is accomplished by appending an ellipsis (see
# https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#detailed-notes). # noqa
key = key.tuple + (Ellipsis,)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two questions:

  1. does this also work in the other branches, i.e., the ones that do fancy indexing?
  2. (related) can you remove the _ensure_ndarray method above?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. It does not affect outer indexing but breaks vectorized indexing, where some arrays come back with dimensions in a different order. I haven't looked into exactly why. It would be nice to move the change into __getitem__, as return array[key + (Ellipsis,)], but I'm afraid that won't work. In any case, I think only basic indexing ever returns a 0d array, so the current change covers all relevant cases.

  2. In fact, yes. I was convinced there were edge cases with object arrays etc., so I let it stay, but all tests pass without. Removed now.

@shoyer shoyer merged commit ce52341 into pydata:master Dec 22, 2018
@shoyer
Copy link
Member

shoyer commented Dec 22, 2018

wonderful, thanks @danielwe !

dcherian pushed a commit to yohai/xarray that referenced this pull request Jan 2, 2019
* master:
  DEP: drop python 2 support and associated ci mods (pydata#2637)
  TST: silence warnings from bottleneck (pydata#2638)
  revert to dev version
  DOC: fix docstrings and doc build for 0.11.1
  Source encoding always set when opening datasets (pydata#2626)
  Add flake check to travis (pydata#2632)
  Fix dayofweek and dayofyear attributes from dates generated by cftime_range (pydata#2633)
  silence import warning (pydata#2635)
  fill_value in shift (pydata#2470)
  Flake fixed (pydata#2629)
  Allow passing of positional arguments in `apply` for Groupby objects (pydata#2413)
  Fix failure in time encoding for pandas < 0.21.1 (pydata#2630)
  Fix multiindex selection (pydata#2621)
  Close files when CachingFileManager is garbage collected (pydata#2595)
  added some logic to deal with rasterio objects in addition to filepaths (pydata#2589)
  Get 0d slices of ndarrays directly from indexing (pydata#2625)
  FIX Don't raise a deprecation warning for xarray.ufuncs.{angle,iscomplex} (pydata#2615)
  CF: also decode time bounds when available (pydata#2571)
@keewis keewis linked an issue Mar 10, 2021 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Indexing not properly working with object dtype element ?
4 participants