In a PY3 session:
In [62]: np.frombuffer('hello world')
...
AttributeError: 'str' object has no attribute '__buffer__'
In [63]: np.frombuffer(b'hello world')
...
ValueError: buffer size must be a multiple of element size
In [64]: np.frombuffer(b'hello world',dtype='S1')
Out[64]:
array([b'h', b'e', b'l', b'l', b'o', b' ', b'w', b'o', b'r', b'l', b'd'], dtype='|S1')
In PY3, the default string type is unicode. The b is used to create and display bytestrings.
The np.frombuffer docs should be updated to reflect the difference. The 'hello world' example only works with PY2 or with PY3 bytestrings.
As I noted in the comments, there are few SO questions regarding frombuffer, indicating that it is rarely used. np.array is by far the most common way of making an array, even from strings:
In [80]: np.array('hello')
Out[80]:
array('hello',
dtype='<U5')
or use list to split the string into characters:
In [81]: np.array(list('hello'))
Out[81]:
array(['h', 'e', 'l', 'l', 'o'],
dtype='<U1')
In [82]: np.array(b'hello')
Out[82]:
array(b'hello',
dtype='|S5')
In [83]: np.array(list(b'hello'))
Out[83]: array([104, 101, 108, 108, 111])
In [85]: np.fromiter('hello','S1')
Out[85]:
array([b'h', b'e', b'l', b'l', b'o'],
dtype='|S1')
In [86]: np.fromiter('hello','U1')
Out[86]:
array(['h', 'e', 'l', 'l', 'o'],
dtype='<U1')*
I created a bug issue: https://github.com/numpy/numpy/issues/8933
frombuffer? That's not a beginners tool.frombufferdocs has an example like this, but it needs to be refined for py3 use.frombufferis. Creatingsas a bytestring might work. I can't test from here.