[Numpy-discussion] Empty strings not empty?

Matthew Brett matthew.brett@gmail....
Thu Dec 31 04:13:51 CST 2009


On Thu, Dec 31, 2009 at 2:08 AM, Christopher Barker
<Chris.Barker@noaa.gov> wrote:
> Charles R Harris wrote:
>> That is due to type promotion for the ufunc call:
>> In [17]: a1 = np.array('a\x00\x00\x00')
>> n [21]: np.array(['a'], dtype=a1.dtype)[0]
>> Out[21]: 'a'
>> In [22]: np.array(['a'], dtype=a1.dtype).tostring()
>> Out[22]: 'a\x00\x00\x00'
> it took me a bit to figure out what this meant, so in case I'm not the
> only one, I thought I'd spell it out:

I think the summary here is 'numpy strings are zero padded; therefore
you may run into surprises with a string that has trailing zeros'.

I see why that is - the zero terminator is the only way for numpy
arrays to see where the end of the string is...



More information about the NumPy-Discussion mailing list