I have created a numpy array (consider input data) and would like to change the order (output data after some numerical operations). While using converted array, I am getting below error and found the root cause. Please find the details below and using numpy version 1.19.1
import numpy as np
a = np.array([1,2,3])
b = np.array([(1.5,2,3), (4,5,6)], dtype = [('col1','<f8'),('col2','<i4'),('col3','<i4')])
names_list=['col2','col1','col3']
c=b[names_list]
print(c.dtype.descr)
But I am getting below error after changing the order of the fields.
lib\site-packages\numpy\core_internal.py", line 114, in _array_descr raise ValueError( ValueError: dtype.descr is not defined for types with overlapping or out-of-order fields
I have done some analysis and found that "offsets" value causing this problem. So i displayed the offsets after order change as below,
{'names':['col2','col1','col3'], 'formats':['<i4','<f8','<i4'], 'offsets':[8,0,12], 'itemsize':16}
and then opened numpy library code in lib\site-packages\numpy\core_internal.py,
offset = 0
for field in ordered_fields:
if field[1] > offset:
num = field[1] - offset
result.append(('', '|V%d' % num))
offset += num
elif field[1] < offset:
raise ValueError(
"dtype.descr is not defined for types with overlapping or "
"out-of-order fields")
Thanks in advance !