[Numpy-discussion] problem with view() and strided arrays?

Zachary Pincus zachary.pincus@yale....
Thu Apr 24 10:41:21 CDT 2008


Hello all,

I'm writing up a general function to allocate aligned numpy arrays  
(I'll post it shortly, as Anne suggested that such a function would be  
useful).

However, I've run into trouble with using ndarray.view() in odd corner- 
cases:
In : numpy.__version__
Out: '1.1.0.dev5077'

In : a = numpy.ones((3,8),dtype=numpy.uint8)
In : a.view(numpy.uint16)
Out:
array([[257, 257, 257, 257],
        [257, 257, 257, 257],
        [257, 257, 257, 257]], dtype=uint16)

In : a = numpy.ones((3,9),dtype=numpy.uint8)
In : a[:,1:].view(numpy.uint16)
ValueError: new type not compatible with array.

In : a[:,:-1].view(numpy.uint16)
ValueError: new type not compatible with array.

In : numpy.array(a[:,:-1]).view(numpy.uint16)
Out:
array([[257, 257, 257, 257],
        [257, 257, 257, 257],
        [257, 257, 257, 257]], dtype=uint16)

It seems like 'view' won't create a view where the stride length is  
not a whole multiple of the dtype's itemsize. However, since strides  
are measured in bytes (right?), this shouldn't be a problem.

Is this a minor bug? Or am I woefully misunderstanding the issue  
involved here?

Thanks,
Zach


More information about the Numpy-discussion mailing list