[Numpy-discussion] recarray field names

Erin Sheldon esheldon at kicp.uchicago.edu
Tue Jul 11 00:01:57 CDT 2006



On 7/11/06, Travis Oliphant <oliphant.travis at ieee.org> wrote:
> Erin Sheldon wrote:
> > Just tested the lastest SVN and it works as advertised.  Thanks
> > Travis.
> >
> > An unrelated question: why does this work for arrays but not recarrays?
> >
> >
> > In [24]: mydescriptor =
> > [('age',float64),('Nchildren',int8),('weight',float32)]
> >
> > In [25]: a = array([(64,2,75.0),(25,0,60.0)], dtype=mydescriptor)
> >
> > In [26]: a = recarray([(64,2,75.0),(25,0,60.0)], dtype=mydescriptor)
> > ---------------------------------------------------------------------------
> > exceptions.TypeError                                 Traceback (most recent
> > call last)
> >
> > /Users/esheldon/<ipython console>
> >
> > TypeError: __new__() got an unexpected keyword argument 'dtype'
> >
> > I understand that I could use the formats and names keywords, but
> > this seems a little inconsistent.
> >
> >
> Well there are a couple of reasons this doesn't work.
> 
> 1) the recarray constructor is similar to the ndarray constructor.
> 
>    Neither of these take list arguments as inputs.  So, while I've added
> dtype as the
>    keyword for the formats input of the recarray constructor, the second
> example will still fail.
> 
> 2) Even were you to use the rec.array function (which is analagous to
> the numpy.array function) to produce the error, it does not take a dtype
> keyword.
> 
>    This is inconsistent.  But, the interface for the record array class
> came from numarray.  I tried to stay compatible with those functions so
> the numarray people would have an easier time adapting.

I see.

> 
>    Perhaps we should add a dtype keyword argument to all the
> constructors in numpy/core/records.py which over-rides the formats,
> names, and titles keywords so that you could do
> 
> a = rec.array([...], dtype=mine)
> 
> analgously to
> 
> a = array([...], dtype=mine)
> 

The advantage of this is the ability to initialize the memory.  One can get
this functionality with core.records.fromrecords, but that is buried pretty
far down.

I realize now that all the functionality is in the array object.  Other than
the .field() method, one gets (I think) all the functionality from using the
above array declaration and then a repeat() call to get a record array.  I
suppose then that recarray is really only for compatibility except for .field()
Is there a compelling reason not to have a .field() method for arrays so that
people in the know can just skip recarray altogether?


>>> mydescriptor = [('age',int16),('Nchildren',int16),('weight',float32)]
>>> a = array([(0,0,0.0)], dtype=mydescriptor)
>>> a = repeat(a, num)





More information about the Numpy-discussion mailing list