[Numpy-discussion] array with object

Robert Kern robert.kern at gmail.com
Thu Nov 30 02:57:17 CST 2006


Lionel Roubeyrie wrote:
> 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)
> ########################################

The latter has a very clear job: take an array of known shape and type and
convert it to a different type. The array() constructor has to divine the shape,
too, with a possibly weird nesting of sequences. Help the poor thing out, and
always be explicit.

> 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 ?

Yes. When object arrays (or record arrays with object columns) are involved, it
is extremely difficult for array() to know what you want to be in the array and
what is simply "structure". Remember that array() is trying to divine both the
shape of the array and the contents at the same time. The mind-reading feature
has not yet been implemented. In its stead, an arbitrary choice was made: tuples
are intended to be elements of record arrays; lists are "structure".

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco


More information about the Numpy-discussion mailing list