[Numpy-discussion] Empty strings not empty?

David Cournapeau cournape at gmail.com
Tue Dec 29 18:44:08 EST 2009


On Wed, Dec 30, 2009 at 8:35 AM, Matthew Brett <matthew.brett at gmail.com> wrote:
> Hi,
>
> I was surprised by this - should I have been?
>
> In [35]: e = np.array(['a'])
>
> In [36]: e.shape
> Out[36]: (1,)
>
> In [37]: e.size
> Out[37]: 1
>
> In [38]: e.tostring()
> Out[38]: 'a'
>
> In [39]: f = np.array(['a'])
>
> In [40]: f.shape == e.shape
> Out[40]: True
>
> In [41]: f.size == e.size
> Out[41]: True
>
> In [42]: f.tostring()
> Out[42]: 'a'
>
> In [43]: z = np.array(['\x00'])
>
> In [44]: z.shape
> Out[44]: (1,)
>
> In [45]: z.size
> Out[45]: 1
>
> In [46]: z
> Out[46]:
> array([''],
>      dtype='|S1')
>
> That is, an empty string array seems to be the same as a string array
> with a single 0 byte, including having shape (1,) and size 1...

I don't see any empty string in your code ? They all have one byte.
The last one is slightly confusing as far as printing is concerned (I
would have expected array(["¥x00"]...) instead). It may be a bug in
numpy because a byte with value 0 is used a string delimiter in C.

cheers,

David



More information about the NumPy-Discussion mailing list