spdl.io.load_npy

load_npy(data: bytes | bytearray, *, copy: bool = False) ndarray[tuple[int, ...], dtype[_ScalarType_co]][source]

Load NumPy NDArray from bytes.

This function loads NumPy NDArray from memory. It is equivalent to numpy.load(io.BytesIO(data)), but it is more efficient.

Note

This function does not support object dtype, and Fortran order.

Example

>>> ref = np.arange(20)
>>> buffer = BytesIO()
>>> np.save(buffer, ref)
>>> buffer.seek(0)
>>> data = buffer.getvalue()
>>> restore = spdl.io.load_npy(data)
>>> assert np.array_equal(restore, ref)
Parameters:
  • data – The data generated by numpy.save() function. (Note that it is different from numpy.ndarray.tobytes(), which does not contain shape and dtype.)

  • copy – Whether to copy data. Default: no copy.

Returns:

The restored array data.

See also

Note

There is a branch in _read_array function where the execution can call some faster implementation. Howver, BytesIO does not meet the condition. (isfileobj() function returns False for io.BytesIO. [source]) Even if the execution takes the faster numpy.fromfile() path, it creates a new array.