[Numpy-discussion] indexing of rank-0 structured arrays: why not?

Robert Kern robert.kern at gmail.com
Mon Jan 10 14:16:02 EST 2011


On Mon, Jan 10, 2011 at 12:15, Nils Becker <n.becker at amolf.nl> wrote:
> Robert,
>
> your answer does work: after indexing with () I can then further index
> into the datatype.
>
> In [115]: a_rank_0[()][0]
> Out[115]: 0.0
>
> I guess I just found the fact confusing that a_rank_1[0] and a_rank_0
> compare and print equal but behave differently under indexing.

They do not print equal. Many things compare equal but do not behave the same.

> More precisely if I do
> In [117]: b = a_rank_1[0]
>
> then
>
> In [118]: b.shape
> Out[118]: ()
>
> and
>
> In [120]: a_rank_0 == b
> Out[120]: True
>
> but
>
> In [119]: b[0]
> Out[119]: 0.0
>
> works but a_rank_0[0] doesn't. I thought b is a rank-0 array which it
> apparently is not since it can be indexed. So maybe b[0] should fail for
> consistency?

No, b is a record scalar. It can be indexed because it is often
convient to treat such records like tuples. This replaces the default
indexing behavior of scalars (which is to simply disallow indexing).
a_rank_0 is an array, so the array indexing semantics are the default,
and we do not change them.

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