I haven't used frombuffer
much, but I think the np.array
works with those arrays as it does with conventionally constructed ones.
Each column_data
array will have its own data buffer - the mmap you assigned it. But np.array(columns)
reads the values from each array in the list, and constructs a new array from them, with its own data buffer.
I like to use x.__array_interface__
to look at the data buffer location (and to see other key attributes). Compare that dictionary for each element of columns
and for data
.
You can construct a 2d array from a mmap - using a contiguous block. Just make the 1d frombuffer
array, and reshape
it. Even transpose
will continue to use that buffer (with F
order). Slices and views also use it.
But unless you are real careful you'll quickly get copies that put the data elsewhere. Simply data1 = data+1
makes a new array, or advance indexing data[[1,3,5],:]
. Same for any concatenation
.
2 arrays from bytestring buffers:
In [534]: x=np.frombuffer(b'abcdef',np.uint8)
In [535]: y=np.frombuffer(b'ghijkl',np.uint8)
a new array by joining them
In [536]: z=np.array((x,y))
In [538]: x.__array_interface__
Out[538]:
{'data': (3013090040, True),
'descr': [('', '|u1')],
'shape': (6,),
'strides': None,
'typestr': '|u1',
'version': 3}
In [539]: y.__array_interface__['data']
Out[539]: (3013089608, True)
In [540]: z.__array_interface__['data']
Out[540]: (180817384, False)
the data buffer locations for x,y,z
are totally different
But the data for reshaped x
doesn't change
In [541]: x.reshape(2,3).__array_interface__['data']
Out[541]: (3013090040, True)
nor does the 2d transpose
In [542]: x.reshape(2,3).T.__array_interface__
Out[542]:
{'data': (3013090040, True),
'descr': [('', '|u1')],
'shape': (3, 2),
'strides': (1, 3),
'typestr': '|u1',
'version': 3}
Same data, different view
In [544]: x
Out[544]: array([ 97, 98, 99, 100, 101, 102], dtype=uint8)
In [545]: x.reshape(2,3).T
Out[545]:
array([[ 97, 100],
[ 98, 101],
[ 99, 102]], dtype=uint8)
In [546]: x.reshape(2,3).T.view('S1')
Out[546]:
array([[b'a', b'd'],
[b'b', b'e'],
[b'c', b'f']],
dtype='|S1')