[Numpy-discussion] rank-0 chararrays?

Todd Miller jmiller at stsci.edu
Tue Feb 18 04:28:03 CST 2003


Francesc Alted wrote:

>Hi,
>
>I'm trying to map Numeric character typecode ('c') to chararrays, but I have
>a problem to distinguish between
>
>In [109]: chararray.array("qqqq")
>Out[109]: CharArray(['qqqq'])
>
>and
>
>In [110]: chararray.array(["qqqq"])  # Note the extra "[" "]"
>Out[110]: CharArray(['qqqq'])   # The same result as 109
>  
>
The chararray API pre-dates our awareness, ultimate implemenation, and 
final rejection of rank-0 arrays.  In retrospect,  your usage above 
makes sense.  Whether we change things now or not is another matter. 
 You are giving me interface angst...  :)

You can create rank-0 arrays by specifying shape=() and 
itemsize=len(buffer).  However, these do not repr correctly (unless you 
update from CVS).

>while in Numeric we have:
>
>In [113]: Numeric.array("qqqq")
>Out[113]: array([q, q, q, q],'c')
>
>In [114]: Numeric.array(["qqqq"])
>Out[114]: array([       [q, q, q, q]],'c')  # Differs from 113
>
>even in numarray objects, rank-0 seems to work well:
>
>In [107]: numarray.array(1)
>Out[107]: array(1)
>
>In [108]: numarray.array([1])
>Out[108]: array([1])   # Objects differ
>
This was not always so,  be we made it work when we thought rank-0 had 
something to offer.  After some discussion on numpy-discussion-list, 
 rank-0 went out of vogue.

>
>
>So, it seems like if chararray does not support well rank-0 objects. 
>
That is true.  CharArray never caught up because rank-0 became vestigal 
even for NumArray.

>Is this
>the expected behavior?. 
>
Yes.  But, rank-0 support for chararray is not far off,  with the 
possible exception of breaking the public interface.

>If yes, we have no possibility to distinguish
>between object 109 and 110, and I'd like to distinguish between this two.
>
Why exactly do you need rank-0?

>What can be done to achieve this?
>
1. Add a little special casing to chararray._charArrayToStringList() to 
handle rank-0.  I did this already in CVS.
2. Debate whether or not to change chararray.array() to work as you've 
shown above.  Proceed from there.

>
>Thanks,
>
>  
>







More information about the Numpy-discussion mailing list