I am trying to wrap my head around the numpy implementation of PEP3118. How exactly does buffer access work in numpy.
>>> p = numpy.getbuffer(numpy.arange(10))
>>> p
<read-write buffer for 0x1003e5b10, size -1, offset 0 at 0x1016ab4b0>
>>> numpy.frombuffer(p)
array([ 0.00000000e+000, 4.94065646e-324, 9.88131292e-324,
1.48219694e-323, 1.97626258e-323, 2.47032823e-323,
2.96439388e-323, 3.45845952e-323, 3.95252517e-323,
4.44659081e-323])
So I am getting unexpected returns. I would expect to see an array with 10 elements from 0-9. I can get into the array and read/write though.
>>> j = numpy.frombuffer(p)
>>> j
array([ 0.00000000e+000, 4.94065646e-324, 9.88131292e-324,
1.48219694e-323, 1.97626258e-323, 2.47032823e-323,
2.96439388e-323, 3.45845952e-323, 3.95252517e-323,
4.44659081e-323])
>>> j += 1
>>> j
array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
So it looks like the buffer is initializing to all zeros, which I can then write to. The functionality that I am expecting is to be able to build the array (with arange or asarray) directly to the buffer with getbuffer. Is that not possible?
The Numpy frombuffer() is one of the predefined function that is used to create the array using the buffer storage with specific areas; mainly, this buffer function is creating the arrays with a different set of parameters it returns the array version of the buffer the python interpreter of the numpy frombuffer() ...
Construct Python bytes containing the raw data bytes in the array. Constructs Python bytes showing a copy of the raw contents of data memory. The bytes object can be produced in either 'C' or 'Fortran', or 'Any' order (the default is 'C'-order).
You have a simple dtype
problem. The buffer you create with
np.getbuffer(np.arange(10))
has a dtype=int
, because np.arange
uses dtype=int
by default.
Then, when you try to read your buffer with
np.frombuffer(p)
you're in fact using the dtype=float
default of np.frombuffer
. Instead, use
np.frombuffer(p, dtype=int)
et voilà, you get
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With