Description
I have spent about an hour trying to hunt down a numpy array construction error. I must not be using numpy dtypes correctly but the error message is not descriptive enough and I am not given a good enough stack trace to find the error.
Simplified Example that creates same error:
import numpy as np
names = ['id', 'x']
formats = [np.int64, np.float64]
np.array([1, 1.0], dtype={'names': names, 'formats': formats})
The following code results in the error
----> 1 np.array([1, 1.0], dtype={'names': names, 'formats': formats})
TypeError: a bytes-like object is required, not 'int'
So i get that the error occurs from the first element being an int but why is it expecting a bytes like object?
Answer: it has nothing to do with the fact that the first element is in int. The list needs to be a tuple see below.
The solution is that numpy requires a single tuple or list of tuples. Not a list of lists or list.
The following works
np.array((1, 1.0), dtype={'names': names, 'formats': formats})
I find it frustrating that numpy gives no indication in the error that this is what is expected.
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