[Numpy-discussion] Incorrect removal of NULL char in buffers

Francesc Altet faltet at carabos.com
Fri Sep 29 11:19:01 EDT 2006


Hi,

I'm trying to build-up numpy arrays coming from buffers, and I'm getting a 
somewhat unexpected result.

First, for numeric values, everything seems ok (i.e. the NULL character is 
correctly interpretated), and works equally for both numarray and numpy:

In [98]: numarray.array("a\x00b"*4, dtype='Float32',shape=3)
Out[98]: array([  2.60561966e+20,   8.94319890e-39,   5.92050103e+20], 
type=Float32)

In [99]: numpy.ndarray(buffer="a\x00b"*4, dtype='Float32',shape=3)
Out[99]: array([  2.60561966e+20,   8.94319890e-39,   5.92050103e+20], 
dtype=float32)

However, for string values, numpy seems to work in a strange way. 
The numarray have an expected behaviour, IMO:

In [100]: numarray.strings.array(buffer="a\x00b"*4, itemsize=4, shape=3)
Out[100]: CharArray(['a', '', 'ba'])  

but numpy  haven't:

In [101]: numpy.ndarray(buffer="a\x00b"*4, dtype="S4", shape=3)
Out[101]:
array([aba, ba, bab],
      dtype='|S4')

i.e. it seems like numpy is striping-off NULL chars before building the object 
and I don't think this is correct.

Cheers,

-- 
>0,0<   Francesc Altet     http://www.carabos.com/
V   V   Cárabos Coop. V.   Enjoy Data
 "-"




More information about the NumPy-Discussion mailing list