[Numpy-discussion] array with object

Lionel Roubeyrie lroubeyrie at limair.asso.fr
Thu Nov 30 02:39:15 CST 2006


Hi Torgil,
strange, I don't have the same error:
########################################
|~|[2]>array(['2'],dtype=float32)
---------------------------------------------------------------------------
exceptions.ValueError                                Traceback (most recent 
call last)
/home/lionel/<ipython console>
ValueError: setting an array element with a sequence.
########################################

I don't understand why there is not an automatic casting, because using astype 
works:
########################################
|~|[3]>a=array(['2'])
|~|[4]>a
Out [4]:
array(['2'], dtype='|S1')
|~|[5]>a.astype(float32)
Out [5]:array([ 2.], dtype=float32)
########################################

Another strange thing, if I use floats instead of strings values, but set them 
in a list of list instead of a list of tuple, I get the same error as in my 
previous post:
########################################
|~|[7]>b=array([(datetime.datetime(2006,11,29),2,3),
(datetime.datetime(2006,11,30),5,6)], dtype=[('Dates', 'object'),
('HUM', 'float32'), ('TEM', 'float32')])
|~|[8]>b
Out [8]:
array([(datetime.datetime(2006, 11, 29, 0, 0), 2.0, 3.0),
       (datetime.datetime(2006, 11, 30, 0, 0), 5.0, 6.0)],
      dtype=[('Dates', '|O4'), ('HUM', '<f4'), ('TEM', '<f4')])
|~|[9]>b=array([[datetime.datetime(2006,11,29),2,3],
[datetime.datetime(2006,11,30),5,6]], dtype=[('Dates', 'object'),
('HUM', 'float32'),('TEM', 'float32')])
|--------------------------------------------------------------------------- 
exceptions.ValueError                                Traceback (most recent 
call last)
/home/lionel/<ipython console>
ValueError: tried to set void-array with object members using buffer.
########################################

Is it expected ?


Le Mercredi 29 Novembre 2006 22:11, Torgil Svensson a écrit :
> It works if you replace the strings '2','3','5','6' with numbers instead.
>
> This case got a better error-message:
> >>> array(['2'],dtype=float32)
>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: a float is required
>
> //Torgil
>
> On 11/29/06, Lionel Roubeyrie <lroubeyrie at limair.asso.fr> wrote:
> > Hi all,
> > I don't understand why can't I do that:
> > b=array([(datetime.datetime(2006,11,29),'2','3'),
> > (datetime.datetime(2006,11,30),'5','6')], dtype=[('Dates', 'object'),
> > ('HUM', 'float32'), ('TEM', 'float32')])
> > ValueError: tried to set void-array with object members using buffer.
> > Thanks
> >
> > --
> > Lionel Roubeyrie - lroubeyrie at limair.asso.fr
> > LIMAIR
> > http://www.limair.asso.fr
> > _______________________________________________
> > Numpy-discussion mailing list
> > Numpy-discussion at scipy.org
> > http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion

-- 
Lionel Roubeyrie - lroubeyrie at limair.asso.fr
LIMAIR
http://www.limair.asso.fr


More information about the Numpy-discussion mailing list