[Numpy-discussion] Type checking inconsistency

Tony Yu tsyu80@gmail....
Sun Oct 16 11:48:58 CDT 2011


On Sun, Oct 16, 2011 at 12:39 PM, Tony Yu <tsyu80@gmail.com> wrote:

> Hi,
>
> I noticed a type-checking inconsistency between assignments using slicing
> and fancy-indexing. The first will happily cast on assignment (regardless of
> type), while the second will throw a type error if there's reason to believe
> the casting will be unsafe. I'm not sure which would be the "correct"
> behavior, but the inconsistency is surprising.
>
> Best,
> -Tony
>
> Example:
>
> >>> import numpy as np
> >>> a = np.arange(10)
> >>> b = np.ones(10, dtype=np.uint8)
>
> # this runs without error
> >>> b[:5] = a[:5]
>
> >>> mask = a < 5
> >>> b[mask] = b[mask]
> TypeError: array cannot be safely cast to required type
>
> And I just noticed that 1D arrays behave differently than 2D arrays. If you
replace the above definitions of a, b with:

>>> a = np.arange(10)[:, np.newaxis]
>>> b = np.ones((10, 1), dtype=np.uint8)

 The rest of the code will run without error.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20111016/2110b797/attachment.html 


More information about the NumPy-Discussion mailing list