[Numpy-discussion] Should the fields in void scalars be available?

Robert Kern robert.kern@gmail....
Fri Jun 27 14:34:40 CDT 2008

On Fri, Jun 27, 2008 at 10:27, Charles R Harris
<charlesr.harris@gmail.com> wrote:
> Question,
> In [24]: x = array(('1', u'2'), dtype=[('a', '|S4'), ('b', '<U2')])
> In [25]: x.shape
> Out[25]: ()
> In [26]: x['a']
> Out[26]:
> array('1',
>       dtype='|S4')
> Shouldn't the last be a string?


In [6]: x = array(('1', u'2', 3), dtype=[('a', '|S4'), ('b', '<U2'),
('c', 'i4')])

In [7]: x
array(('1', u'2', 3),
      dtype=[('a', '|S4'), ('b', '<U2'), ('c', '<i4')])

In [8]: x['c']
Out[8]: array(3)

x is not a scalar. It is a rank-0 array. Picking out a field from it
will also be a rank-0 array. If x were a rank-1 array, picking out a
field from it will give a rank-1 array.

In [12]: y = array([('1', u'2', 3)], dtype=[('a', '|S4'), ('b',
'<U2'), ('c', 'i4')])

In [13]: y['a']

The only inconsistency I can see here is that x.item() gives a tuple
rather than a scalar record. A scalar record does have field access,
but the tuple doesn't.

In [28]: x.item()['a']
TypeError                                 Traceback (most recent call last)

/Users/rkern/bzr/ipython/<ipython console> in <module>()

TypeError: tuple indices must be integers

In [29]: y[0]['a']
Out[29]: '1'

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