Use to_numpy in ImplicitToExplicitIndexingAdapter.__array__ method#11381
Open
weiji14 wants to merge 5 commits into
Open
Use to_numpy in ImplicitToExplicitIndexingAdapter.__array__ method#11381weiji14 wants to merge 5 commits into
weiji14 wants to merge 5 commits into
Conversation
Fixes `TypeError: Implicit conversion to a NumPy array is not allowed. Please use `.get()` to construct a NumPy array explicitly` when using a backend engine that loads data onto CuPy, but indexes should stay as NumPy.
dcherian
approved these changes
Jun 12, 2026
Contributor
Author
|
Should probably add a test for this somehow. |
Ensure that instance of ImplicitToExplicitIndexingAdapter's __array__ method works on duck arrays, tested by passing it through np.asarray.
weiji14
commented
Jun 12, 2026
Comment on lines
+690
to
+691
| if Version(np.__version__) >= Version("2.0.0"): | ||
| return np.asarray(self.get_duck_array(), dtype=dtype, copy=copy) | ||
| return to_numpy(self.get_duck_array(), dtype=dtype, copy=copy) |
Contributor
Author
There was a problem hiding this comment.
This fixes the TypeError, but I don't think it is the correct solution (to_numpy doesn't handle the dtype/copy arguments). Probably should use duck_array_ops.as_array instead? Also, best if we can drop NumPy<2.0 to make the code cleaner 🙂
Contributor
Author
There was a problem hiding this comment.
Ok, using np.asarray(to_numpy(self.get_duck_array()), dtype=dtype, copy=copy) now, which should be the correct way.
So that the dtype and copy arguments are handled by np.asarray.
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes
TypeError: Implicit conversion to a NumPy array is not allowed. Please use `.get()` to construct a NumPy array explicitlywhen using a backend engine that loads data onto CuPy, but indexes should stay as NumPy (following convention at xarray-contrib/cupy-xarray#90)Full traceback
Somewhat similar to #10078. Helps with some cupy-xarray work that is making TIFF to GPU possible at xarray-contrib/cupy-xarray#81 (comment) (though not fully working yet 🙂).
Checklist
whats-new.rstapi.rstAI Disclosure