[Numpy-discussion] np.zeros(2, 'S') returns empty strings.
josef.pktd@gmai...
josef.pktd@gmai...
Sun Jan 15 07:00:57 CST 2012
On Sun, Jan 15, 2012 at 3:15 AM, Nathaniel Smith <njs@pobox.com> wrote:
> On Sat, Jan 14, 2012 at 2:12 PM, Charles R Harris
> <charlesr.harris@gmail.com> wrote:
>> This sort of makes sense, but is it the 'correct' behavior?
>>
>> In [20]: zeros(2, 'S')
>> Out[20]:
>> array(['', ''],
>> dtype='|S1')
>
> I think of numpy strings as raw fixed-length byte arrays (since, well,
> that's what they are), so I would expect np.zeros to return all-NUL
> strings, like it does. (Not just 'empty' strings, which just means the
> first byte is NUL -- I expect all-NUL.) Maybe I've spent too much time
> working with C data structures, but that's my $0.02 :-)
Since I'm not coding in C: can a fixed-length empty string, '', be
represented as only first byte is NUL?
The following with the current behavior looks all reasonable to me
>>> np.zeros(2).view('S4')
array(['', '', '', ''],
dtype='|S4')
>>> np.zeros(4, 'S4').view(float)
array([ 0., 0.])
>>> np.zeros(4, 'S4').view(int)
array([0, 0, 0, 0])
>>> np.zeros(4, 'S4').view('S16')
array([''],
dtype='|S16')
np.zeros(2, float).view('S4')
array(['', '', '', ''],
dtype='|S4')
instead of astype
>>> np.zeros(2, float).astype('S4')
array(['0.0', '0.0'],
dtype='|S4')
my 2c (with trying to understand what's the question)
Josef
>
> -- Nathaniel
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
More information about the NumPy-Discussion
mailing list