[Numpy-discussion] numpy array casting ruled not safe

Charles R Harris charlesr.harris at gmail.com
Sat Mar 7 16:52:16 EST 2015


On Sat, Mar 7, 2015 at 2:45 PM, Charles R Harris <charlesr.harris at gmail.com>
wrote:

>
>
> On Sat, Mar 7, 2015 at 2:02 PM, Dinesh Vadhia <dineshbvadhia at hotmail.com>
> wrote:
>
>>   This was originally posted on SO (
>> https://stackoverflow.com/questions/28853740/numpy-array-casting-ruled-not-safe)
>> and it was suggested it is probably a bug in numpy.take.
>>
>> Python 2.7.8 |Anaconda 2.1.0 (32-bit)| (default, Jul  2 2014, 15:13:35)
>> [MSC v.1500 32 bit (Intel)] on win32
>> Type "copyright", "credits" or "license()" for more information.
>>
>> >>> import numpy
>> >>> numpy.__version__
>> '1.9.2'
>>
>> >>> a = numpy.array([9, 7, 5, 4, 3, 1], dtype=numpy.uint32)
>> >>> b = numpy.array([1, 3], dtype=numpy.uint32)
>> >>> c = a.take(b)
>>
>> Traceback (most recent call last):
>>   File "<pyshell#5>", line 1, in <module>
>>     c = a.take(b)
>> TypeError: Cannot cast array data from dtype('uint32') to dtype('int32')
>> according to the rule 'safe'
>>
>
> This actually looks correct for 32-bit windows. Numpy indexes with a
> signed type big enough to hold a pointer to void, which in this case is an
> int32, and the uint32 cannot be safely cast to that type.
>
> Chuck
>

I note that on SO Jaime made the suggestion that take use unsafe casting
and throw an error on out of bounds indexes. That sounds reasonable,
although for sufficiently large integer types an index could wrap around to
a good value. Maybe make it work only for npy_uintp.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20150307/06f54761/attachment.html>


More information about the NumPy-Discussion mailing list