D
D
Dmitry Temnikov2020-03-10 17:19:24
Python
Dmitry Temnikov, 2020-03-10 17:19:24

Why doesn't NumPy structured array work?

Why is this piece of code

tpl=('07f29c2c-9bd2-4999-b69c-a17d15a90700', datetime.datetime(2019, 8, 18, 0, 0), datetime.datetime(2019, 8, 18, 13, 3, 56), 
datetime.timedelta(seconds=31), datetime.datetime(2019, 8, 18, 13, 4, 27), 57)
dt = np.dtype([('v2m_guid', str, 36), ('start_date', 'datetime64[ns]'), ('start_time', 'datetime64[ns]'), ('voice_length', 'timedelta64[ns]'), ('end_time', 'datetime64[ns]'), ('text_length', 'int32')])
arr=np.array(tpl, dtype=dt) 
arr[0]

When executing the instruction , it gives an error , despite the fact that the array has been created and the variable manager in Spyder sees the created array and allows you to correct it? While the training examplearr[0]IndexError: too many indices for array

In[86]: dt = np.dtype([('name', np.unicode_, 16), ('grades', np.float64, (2,))])
In[87]: x = np.array([('Sarah', (8.0, 7.0)), ('John', (6.0, 7.0))], dtype=dt)
Out[87]: 
array([('Sarah', [8., 7.]), ('John', [6., 7.])],
      dtype=[('name', '<U16'), ('grades', '<f8', (2,))])
In[88]: x[1]
Out[88]: ('John', [6., 7.])

works quite well

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Temnikov, 2020-03-10
@exibit777

the error is confusing the difference in the way the array is specifiedIndexError: too many indices for array

arr=np.array([tpl], dtype=dt)
arr=np.array(tpl, dtype=dt)
In the first case, the elements are accessible through the index In the second case, there are no elements and you can immediately access the fieldsarr[0]["v2m_guid"]
arr["v2m_guid"]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question