numpy.frombuffer#

numpy.frombuffer(buffer,dtype=float,count=-1,offset=0,*,like=None)#

Interpret a buffer as a 1-dimensional array.

Parameters:
bufferbuffer_like

An object that exposes the buffer interface.

dtypedata-type, optional

Data-type of the returned array; default: float.

countint, optional

Number of items to read.-1 means all data in the buffer.

offsetint, optional

Start reading the buffer from this offset (in bytes); default: 0.

likearray_like, optional

Reference object to allow the creation of arrays which are notNumPy arrays. If an array-like passed in aslike supportsthe__array_function__ protocol, the result will be definedby it. In this case, it ensures the creation of an array objectcompatible with that passed in via this argument.

New in version 1.20.0.

Returns:
outndarray

See also

ndarray.tobytes

Inverse of this operation, construct Python bytes from the raw data bytes in the array.

Notes

If the buffer has data that is not in machine byte-order, this shouldbe specified as part of the data-type, e.g.:

>>>dt=np.dtype(int)>>>dt=dt.newbyteorder('>')>>>np.frombuffer(buf,dtype=dt)

The data of the resulting array will not be byteswapped, but will beinterpreted correctly.

This function creates a view into the original object. This should be safein general, but it may make sense to copy the result when the originalobject is mutable or untrusted.

Examples

>>>importnumpyasnp>>>s=b'hello world'>>>np.frombuffer(s,dtype='S1',count=5,offset=6)array([b'w', b'o', b'r', b'l', b'd'], dtype='|S1')
>>>np.frombuffer(b'\x01\x02',dtype=np.uint8)array([1, 2], dtype=uint8)>>>np.frombuffer(b'\x01\x02\x03\x04\x05',dtype=np.uint8,count=3)array([1, 2, 3], dtype=uint8)
On this page