[Numpy-discussion] Accessing elements of an object array

Robert Kern robert.kern at gmail.com
Wed Mar 16 16:20:28 EDT 2011


On Wed, Mar 16, 2011 at 15:18,  <lists_ravi at lavabit.com> wrote:
> Hi,
>  How do I access elements of an object array? The object array was
> created by scipy.io.loadmat from a MAT file. Here's an example:
>
> In [10]: x
> Out[10]:
> array(array((7.399500875785845e-10, 7.721153414752673e-10, -0.984375),
>      dtype=[('cl', '|O8'), ('tl', '|O8'), ('dagc', '|O8')]), dtype=object)
>
> In [11]: x.shape, x.size
> Out[11]: ((), 1)
>
> In [12]: x.flat[0]['cl']
> Out[12]: array(array(7.399500875785845e-10), dtype=object)
>
> In [13]: x[0]
> ---------------------------------------------------------------------------
> IndexError                                Traceback (most recent call last)
>
> /src/<ipython console> in <module>()
>
> IndexError: 0-d arrays can't be indexed

It's not that it's an object array. It's that it is a ()-shape array.
You index it with an empty tuple:

  x[()]

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