On Wed, Aug 12, 2015 at 2:03 PM, Nathan Goldbaum <nathan12...@gmail.com>
wrote:

> Hi all,
>
> I've been testing the package I spend most of my time on, yt, under numpy
> 1.10b1 since the announcement went out.
>
> I think I've narrowed down and fixed all of the test failures that cropped
> up except for one last issue. It seems that the behavior of np.digitize
> with respect to ndarray subclasses has changed since the NumPy 1.9 series.
> Consider the following test script:
>
> ```python
> import numpy as np
>
>
> class MyArray(np.ndarray):
>     def __new__(cls, *args, **kwargs):
>         return np.ndarray.__new__(cls, *args, **kwargs)
>
> data = np.arange(100)
>
> bins = np.arange(100) + 0.5
>
> data = data.view(MyArray)
>
> bins = bins.view(MyArray)
>
> digits = np.digitize(data, bins)
>
> print type(digits)
> ```
>
> Under NumPy 1.9.2, this prints "<type 'numpy.ndarray'>", but under the
> 1.10 beta, it prints "<class '__main__.MyArray'>"
>
> I'm curious why this change was made. Since digitize outputs index arrays,
> it doesn't make sense to me why it should return anything but a plain
> ndarray. I see in the release notes that digitize now uses searchsorted
> under the hood. Is this related?
>

It is indeed searchsorted's fault, as it returns an object of the same type
as the needle (the items to search for):

>>> import numpy as np
>>> class A(np.ndarray): pass
>>> class B(np.ndarray): pass
>>> np.arange(10).view(A).searchsorted(np.arange(5).view(B))
B([0, 1, 2, 3, 4])

I am all for making index-returning functions always return a base ndarray,
and will be more than happy to send a PR fixing this if there is some
agreement.

Jaime

-- 
(\__/)
( O.o)
( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus planes
de dominación mundial.
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to