NumPy – What is the difference between frombuffer and fromstring?

From a practical standpoint, the difference is that:

x = np.fromstring(s, dtype="int8")

Will make a copy of the string in memory, while:

x = np.frombuffer(s, dtype="int8")

or

x = np.frombuffer(buffer(s), dtype="int8")

Will use the memory buffer of the string directly and won’t use any* additional memory. Using frombuffer will also result in a read-only array if the input to buffer is a string, as strings are immutable in python.

(*Neglecting a few bytes of memory used for an additional python ndarray object — The underlying memory for the data will be shared.)


If you’re not familiar with buffer objects (memoryview in python3.x), they’re essentially a way for C-level libraries to expose a block of memory for use in python. It’s basically a python interface for managed access to raw memory.

If you were working with something that exposed the buffer interface, then you’d probably want to use frombuffer. (Python 2.x strings and python 3.x bytes expose the buffer interface, but you’ll get a read-only array, as python strings are immutable.)

Otherwise, use fromstring to create a numpy array from a string. (Unless you know what you’re doing, and want to tightly control memory use, etc.)

Leave a Comment