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

Francesc Altet faltet at carabos.com
Fri Sep 29 10:19:01 CDT 2006


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], 

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

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)
array([aba, ba, bab],

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


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

More information about the Numpy-discussion mailing list