Open
Description
When attempting to create a view of a indexed structured array, in order to create a a standard ndarray, the view method does not appropriately resize the output array. It appears to instead preserve the size of the full structured array.
I have two examples in the code:
- Uses the prescribed methodology from https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.view.html
- Uses column stack to combine each column slice
As you can see, example #1 is the incorrect size and has garbage from memory in the middle column. Example #2 works as expected.
Reproducing code example:
import numpy as np
test_arr = np.array([(1, 2, 3), (4, 5, 6), (7, 8, 9), (10, 11, 12)],
dtype=[('a', np.int8), ('b', np.int8), ('c', np.int8)])
temp_arr = (test_arr[['a', 'c']]
.copy()
.view(np.int8)
.reshape(test_arr.shape + (-1,)))
print('using view:\n', temp_arr)
temp_arr = None
for col_name in ['a', 'c']:
temp_field_arr = test_arr[col_name]
if temp_arr is None:
temp_arr = temp_field_arr.copy()
else:
temp_arr = np.column_stack((temp_arr,
temp_field_arr.copy()))
print('using column_stack:\n', temp_arr)
Numpy/Python version information:
python 3.7.3
numpy 1.16.4
import sys, numpy; print(numpy.version, sys.version)
1.16.4 3.7.3 (default, Mar 27 2019, 22:11:17)
[GCC 7.3.0]