numpy.repeat TypeError: array cannot be safely cast to required type

George Sakkis george.sakkis at gmail.com
Fri Nov 3 14:25:54 CST 2006


Tim Hochberg wrote:

> George Sakkis wrote:
> > Can anyone explain this ?
> >
> >
> >>>> import numpy as N
> >>>> x = N.arange(1,6,dtype='B')
> >>>> x
> >>>>
> > array([1, 2, 3, 4, 5], dtype=uint8)
> >
> >>>> N.repeat(x, N.ones(5,'H'))
> >>>>
> > array([1, 2, 3, 4, 5], dtype=uint8)
> >
> >>>> N.repeat(x, N.ones(5,'l'))
> >>>>
> > array([1, 2, 3, 4, 5], dtype=uint8)
> >
> >>>> N.repeat(x, N.ones(5,'L'))
> >>>>
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in ?
> >   File
> > "/usr/local/lib/python2.4/site-packages/numpy/core/fromnumeric.py",
> > line 83, in repeat
> >     return repeat(repeats, axis)
> > TypeError: array cannot be safely cast to required type
> >
> It means you can't safely a value of type uint32 ('L') to a value of
> type int32 ('i').  The second argument of repeat needs to be castable to
> type int32.

I see. I suppose there is a better reason for this than a "why on earth
would anyone repeat a value more than 2**32-1 times". Besides, why
should it be castable to a signed type since we're talking about number
of repetitions ?

> A style note: please use the named dtypes (int32, uint32, etc) rather
> than the old-style letter codes; the former is much clearer. The answer
> to your question might  have been immediately apparent had you been
> using named dtypes.

I'm using numpy along with the struct module, so the letter codes are
convenient for talking to both modules.

George


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642




More information about the Numpy-discussion mailing list