Indexing one numpy array with another - both are defined as dtype='uint32'. Using numpy.take to index and get an unsafe casting error. Not come across this before. Any idea what is going on?
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.0'
>>> 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#12>", line 1, in <module>
c = a.take(b)
TypeError: Cannot cast array data from dtype('uint32') to dtype('int32') according to the rule 'safe'
a.take(b)toc. Try settingc =numpy.array(0,dtype=numpy.uint32)beforec = a.take(b)a[-1]works like it would in a Python list, after all). When it sees that the index array has the wrong dtype, it tries to convert the index array to an array of signed ints (which would be reasonable and produce reasonable results ifbwere an array of, say,int16). Since this conversion isn't safe for an array ofuint32- for example, a maximaluint32value would become -1, a rather different index - NumPy throws an error. You may want to convert the dtype ofbyourself before usingtake.