[Numpy-discussion] scalar recordarrays

Matthew Koichi Grimes mkg@cs.nyu....
Wed Mar 21 22:42:34 CDT 2007


If there are no objections, I'll file this ticket in the trac site:

<snip>
Title:
Return type inconsistency in recarray

Description:
The sub-arrays of rank-0 recarrays are returned as scalars rather than 
rank-0 ndarrays.

Example:
 >>> import numpy as N
 >>> dt = N.dtype([('x','f8'),('y','f8')])
 >>> rarr = N.zeros((), dtype = dt).view(N.recarray)
 >>> rarr
recarray((0.0, 0.0),
      dtype=[('x', '<f8'), ('y', '<f8')])
 >>> # oddly, rarr.x is not an array
 >>> rarr.x
0.0
 >>> # This makes it impossible to fill values in the usual manner
 >>> rarr.x[...] = 2.0
TypeError: object does not support item assignment
 >>> # A workaround is to reshape to non-zero rank
 >>> rarr.shape = [1]
 >>> # Now rarr.x returns a ndarray of the same rank as rarr
 >>> rarr.x
array([ 0.])
 >>> # Value-setting now works as expected. 
 >>> rarr.x[...] = 2.0
 >>> rarr   
recarray([(2.0, 0.0)],
      dtype=[('x', '<f8'), ('y', '<f8')])

This is a problem for any function that takes record arrays of arbitrary 
rank, as it necessitates special-case checks for ndim == 0.
</snip>

-- Matt

Francesc Altet wrote:

> Yeah, I agree. In fact, one of the main reasons to keep rank-0 arrays
> around is to provide generality enough to address to these kind of
> problems in an elegant way.
>
> Cheers,
>   



More information about the Numpy-discussion mailing list