indexing of rank-0 structured arrays: why not?
Hi, I noticed that I can index into a dtype when I take an element of a rank-1 array but not if I make a rank-0 array directly. This seems inconsistent. A bug? Nils In [76]: np.version.version Out[76]: '1.5.1' In [78]: dt = np.dtype([('x', '<f8'), ('y', '<f8')]) In [80]: a_rank_1 = np.zeros((1,), dtype=dt) In [81]: a_rank_0 = np.zeros((), dtype=dt) In [83]: a_rank_1[0] Out[83]: (0.0, 0.0) In [84]: a_rank_1[0] == a_rank_0 Out[84]: True In [85]: a_rank_1[0][0] Out[85]: 0.0 In [86]: a_rank_0[0] --------------------------------------------------------------------------- IndexError Traceback (most recent call last) /home/pabra/ramp/testramp/<ipython console> in <module>() IndexError: 0-d arrays can't be indexed In [87]: a_rank_0['x'] Out[87]: array(0.0)
On Mon, Jan 10, 2011 at 10:08, Nils Becker <n.becker@amolf.nl> wrote:
Hi,
I noticed that I can index into a dtype when I take an element of a rank-1 array but not if I make a rank-0 array directly. This seems inconsistent. A bug?
Not a bug. Since there is no axis, you cannot use integers to index into a rank-0 array. Use an empty tuple instead. [~] |1> dt = np.dtype([('x', '<f8'), ('y', '<f8')]) [~] |2> a_rank_0 = np.zeros((), dtype=dt) [~] |3> a_rank_0[()] (0.0, 0.0) -- 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
participants (2)
-
Nils Becker -
Robert Kern