[SciPy-User] numpy.array of mixed type.

Éric Depagne edepagne at lcogt.net
Sun Apr 25 13:25:42 EDT 2010


Le dimanche 25 avril 2010 09:41:55, Charles R Harris a écrit :
> On Sun, Apr 25, 2010 at 9:00 AM, <josef.pktd at gmail.com> wrote:
> > On Sun, Apr 25, 2010 at 10:52 AM, Robert Kern <robert.kern at gmail.com>
> >
> > wrote:
> > > On Thu, Apr 22, 2010 at 10:16, Éric Depagne <edepagne at lcogt.net> wrote:
> > >> Le mercredi 21 avril 2010 17:27:14, Charles R Harris a écrit :
> > >>> On Wed, Apr 21, 2010 at 9:49 AM, Éric Depagne <edepagne at lcogt.net>
> >
> > wrote:
> > >>> > Hi.
> > >>> >
> > >>> > I'd like to create an array that would contain data of two
> > >>> > different types: str and float64.
> > >>> >
> > >>> > I've created a dtype accordingly :
> > >>> > dt = dtype({'names': ['Type', 'Chisquare'], 'formats': ['S8',
> >
> > float64]})
> >
> > >>> > then, I initialise my array as follow:
> > >>> > temp = zeros ((1,1), dtype = dt)
> > >>> >
> > >>> > that gives me the following:
> > >>> > array([[('', 0.0)]],
> > >>> >      dtype=[('Type', '|S8'), ('Chisquare', '<f8')])
> > >>> >
> > >>> > which is almost good.
> > >>> >
> > >>> > I'd like to know if it is possible instead of having an array with
> >
> > one
> >
> > >>> > column
> > >>> > that will contain a tuple, to create an array with two columns, 
> > >>> > the first column being a str and the second a float.
> > >>>
> > >>> It's not a tuple, it is just displayed that way, it is more like a
> >
> > packed c
> >
> > >>> structure. But no, you can't have *actual* ndarray columns of
> > >>> different types. What problem do you have with using the dtype?
> > >>
> > >> I tried to sort my array along the Chisquare column and could not.
> > >> temp.sort (axis = 0) sorts along the column named Type, but I can't
> > >> find
> >
> > a way
> >
> > >> to sort along Chisquare. Is it possible?
> > >
> > > Use numpy.lexsort()
> > >
> > > i = numpy.lexsort([myarray['Chisquare']])
> > > sorted_array = myarray[i]
> >
> > I don't know if I'm missing something but the docstring or numpy.sort
> > looks pretty good:
> >
> > order : list, optional
> > When a is a structured array, this argument specifies which fields to
> > compare first, second, and so on. This list does not need to include
> > all of the fields.
> > <...>
> > Use the order keyword to specify a field to use when sorting a structured
> >
> > array:
> > >>> dtype = [('name', 'S10'), ('height', float), ('age', int)]
> > >>> values = [('Arthur', 1.8, 41), ('Lancelot', 1.9, 38),
> >
> > ...           ('Galahad', 1.7, 38)]
> >
> > >>> a = np.array(values, dtype=dtype)       # create a structured array
> > >>> np.sort(a, order='height')
> >
> > array([('Galahad', 1.7, 38), ('Arthur', 1.8, 41),
> >       ('Lancelot', 1.8999999999999999, 38)],
> >      dtype=[('name', '|S10'), ('height', '<f8'), ('age', '<i4')])
> 
> I'll bet lexsort is faster, though.
> 
> Chuck
> 
Thanks Robert for the lexsort, Josef for the order option of numpy.sort (how 
did I miss it ?) and Chuck for a way to choose between them!

Éric.
-- 
Un clavier azerty en vaut deux
----------------------------------------------------------
Éric Depagne                            edepagne at lcogt.net
Las Cumbres Observatory 
6740 Cortona Dr
Goleta CA, 93117
----------------------------------------------------------



More information about the SciPy-User mailing list