4

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?

JRG
  • 4,037
  • 3
  • 23
  • 34
lumbric
  • 7,644
  • 7
  • 42
  • 53
  • Does https://stackoverflow.com/questions/19201868/how-to-set-dtype-for-nested-numpy-ndarray answer your question? – Eric Jul 14 '17 at 15:24

2 Answers2

4

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']

Eric
  • 95,302
  • 53
  • 242
  • 374
  • The data for `cars` has to be properly nested as you (right mix of [] and ()). That can be a source of errors. But when loading from a `csv` `genfromtxt` can use a 'flat' list of columns, as long as there's enough data. – hpaulj Jul 14 '17 at 16:23
  • Just checked: `genfromtxt` first creates an array with a flattened version of the `dtype`, and then returns a view with the nested dtype. – hpaulj Jul 14 '17 at 16:43
  • Can this be done with lists of different lengths? E.g. how would it look like if I want to add a field ``photo`` to the ``car_dt`` which is a 2-dimensional array of unknown size and type int? (let's use a black/white image for simplicity) – lumbric Jul 19 '17 at 09:22
  • No, numpy has no mechanism for subarrays of "unknown size". You'll have to use an `object` field to store that array – Eric Jul 19 '17 at 10:02
-2

You can construct nested arrays the same way you construct nested lists:

nested_list = [['a',1],['b',2],['c',3]]

import numpy as np
nested_array = np.array(nested_list)
artbn
  • 77
  • 5