Skip to content Skip to sidebar Skip to footer

Numpy Frombuffer - Attributeerror: 'str' Object Has No Attribute '__buffer__'

Python Version: 3.5.2 Numpy Version : 1.12.1 Error: import numpy as np s = 'Hello World' np.frombuffer(s, dtype='S1') AttributeError: 'str' object has no attribute '__buffer__' Th

Solution 1:

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

Post a Comment for "Numpy Frombuffer - Attributeerror: 'str' Object Has No Attribute '__buffer__'"