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

ENH: Support obects with __array__ method #573

Closed
snowman2 opened this issue Mar 11, 2020 · 9 comments · Fixed by #625
Closed

ENH: Support obects with __array__ method #573

snowman2 opened this issue Mar 11, 2020 · 9 comments · Fixed by #625
Labels
enhancement proposal Idea for a new feature.

Comments

@snowman2
Copy link
Member

https://docs.scipy.org/doc/numpy/reference/arrays.interface.html

Had the thought, putting here to look into.

@snowman2 snowman2 added the proposal Idea for a new feature. label Mar 11, 2020
@mraspaud
Copy link
Contributor

Yes, it would be great to have support for xarray.DataArray or dask.array.Array objects!

@snowman2
Copy link
Member Author

snowman2 commented May 5, 2020

I did some looking into xarray.DataArray and is seems it does not have __array_interface__. However, it does have __array__. @mraspaud did you have any thoughts on what behavior you would like to see there?

@snowman2 snowman2 changed the title Support object with __array_interface__ as input ENH: Support object with __array_interface__ as input May 5, 2020
@mraspaud
Copy link
Contributor

mraspaud commented May 5, 2020

Oh, sorry, I hadn't checked that xarray had this interface. Anyway, our use case is that we have xarray.DataArray structures containing dask.array.Array objects. Passing any of these to a Proj object just fails at the moment:

 In [1]: import xarray as xr                                                     

In [2]: from pyproj import Proj                                                 

In [6]: prj(xr.DataArray([1, 2, 3]), xr.DataArray([1, 2, 3]))                   

TypeError: input must be an array, list, tuple or scalar

In [7]: import dask.array as da                                                 

In [8]: prj(da.from_array([1, 2, 3]), da.from_array([1, 2, 3]))                 

TypeError: input must be an array, list, tuple or scalar

It would be nice if pyproj wouldn't just choke on these. Just calling eg np.asarray should suffice. Now I realise pyproj uses the underlying proj library and thus converts array to C structures at one point, so it would loose the nice features of xarray or dask along the way, but it would work.

@snowman2
Copy link
Member Author

snowman2 commented May 5, 2020

Okay, so if I interpreted that correctly, you would be okay with getting a numpy array returned?

Next step is to check if dask has the same __array__ method.

@mraspaud
Copy link
Contributor

mraspaud commented May 5, 2020

I'd be ok with that. Maybe @djhoese has an opinion on this ?

@djhoese
Copy link
Contributor

djhoese commented May 5, 2020

Part of me wishes pyproj could magically know how to handle dask arrays in a dask-friendly way (map_blocks wrapper), but I think converting to a numpy array and returning a numpy array matches xarray behavior. For example, when there are methods in xarray that dask doesn't support I believe xarray just converts to numpy and continues on processing.

@snowman2
Copy link
Member Author

snowman2 commented May 7, 2020

Looks like the dask array also has __array__, so I think the code could be updated to just call that function and it would work for both.

@snowman2
Copy link
Member Author

snowman2 commented May 7, 2020

Implemented in #625

@snowman2
Copy link
Member Author

snowman2 commented May 8, 2020

Looks like this will also work with pandas.Series.__array__

@snowman2 snowman2 changed the title ENH: Support object with __array_interface__ as input ENH: Support obects with '__array__' method May 8, 2020
@snowman2 snowman2 changed the title ENH: Support obects with '__array__' method ENH: Support obects with __array__ method May 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement proposal Idea for a new feature.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants