The numpy manual mentions use case for numpy.save
Annie Analyst has been using large nested record arrays to represent her statistical data.
Is it possible to have nested records array without dtype=object? If so, how?
Yes, like so:
engine_dt = np.dtype([('volume', float), ('cylinders', int)])
car_dt = np.dtype([('color', int, 3), ('engine', engine_dt)])  # nest the dtypes
cars = np.rec.array([
    ([255, 0, 0], (1.5, 8)),
    ([255, 0, 255], (5, 24)),
], dtype=car_dt)
print(cars.engine.cylinders)
# array([ 8, 24])
The np.dtype function isn't strictly necessary here, but it's usually a good idea, and gives a small speed boost over letting array call it every time.
Note that rec.array is only necessary here to use the .engine notation. If you used a plain np.array, then you'd use cars['engine']['cylinders']
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