[Numpy-discussion] Type casting problems with numpy.take

Jaime Fernández del Río jaime.frio@gmail....
Fri Feb 8 17:12:33 CST 2013


I see there is a lot of ongoing discussion on casting rules, but I couldn't
find any reference to the following issue I am facing. I am trying to
'take' from an array of uint8's, using an array of uint16's as indices.
Even though the return dtype would be uint8, I want to direct the output
back into the array of uint16's:

>>> lut = np.random.randint(256, size=(65536,)).astype('uint8')
>>> arr = np.random.randint(65536, size=(1000, 1000)).astype('uint16')
>>> np.take(lut, arr, out=arr)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\numpy\core\fromnumeric.py", line 103,
in take
    return take(indices, axis, out, mode)
TypeError: array cannot be safely cast to required type

This is puzzling, since the only casting that should be happening is from
uint8's to uint16's, which is as safe as it gets:

>>> np.can_cast('uint8', 'uint16')

To make things even weirder, I can get the above code to work if the type
of lut is uint16, uint32, uint64, int32 or int 64, but not if it is uint8,
int8 or int16. Without looking at the source, it almost looks as if the
type checking in numpy.take was reversed... Am I missing something, or is
this broken?

My numpy's version is:

>>> np.__version__

which is the one packaged in Python xy, running on a 64 bit Windows
7 system.



P.S. I have posted the same question in StackExchange:

( O.o)
( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus planes
de dominación mundial.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20130208/3aff1b0d/attachment.html 

More information about the NumPy-Discussion mailing list