[Numpy-discussion] Selection of only a certain number of fields
Sat Feb 7 12:17:57 CST 2009
A Saturday 07 February 2009, Neil escrigué:
> Travis E. Oliphant <oliphant <at> enthought.com> writes:
> > I've been fairly quiet on this list for awhile due to work and
> > family schedule, but I think about how things can improve
> > regularly. One feature that's been requested by a few people is
> > the ability to select multiple fields from a structured array.
> > Thus, suppose *arr* is a structured array with dtype:
> > [('name', 'S25'),
> > ('height', float),
> > ('age', int),
> > ('gender', 'S8')
> > ]
> > Then, newarr = arr[['name', 'age']] should be a structured array
> > with just the name and age fields.
> What are some common use cases for this feature?
> I use structured arrays quite a lot, but I haven't found myself
> wanting something like this. If I do need a subset of a structured
> array generally I use something like
> [rec[n] for n in 'name age gender'.split()]
Good point. However, there are still some very valid reasons for having
an idiom like:
newarr = arr[['name', 'age']]
returning a record array.
The first one (and most important IMO), is that newarr continues to be
an structured array (BTW, when changed this name from the original
record array?), and you can use all the features of these beasts with
it. Other reason (albeit a bit secondary) is that its data buffer can
be shared through the array interface with other applications, or plain
C code, in a relatively straightforward way. However, if newarr
becomes a list (or dictionary), this is simply not possible.
More information about the Numpy-discussion