[Numpy-discussion] np.zeros(2, 'S') returns empty strings.
josef.pktd at gmail.com
josef.pktd at gmail.com
Sun Jan 15 08:00:57 EST 2012
On Sun, Jan 15, 2012 at 3:15 AM, Nathaniel Smith <njs at pobox.com> wrote:
> On Sat, Jan 14, 2012 at 2:12 PM, Charles R Harris
> <charlesr.harris at 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 at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
More information about the NumPy-Discussion
mailing list