[Numpy-discussion] Automatic string length in recarray

Thomas Robitaille thomas.robitaille@gmail....
Wed Nov 4 10:35:25 CST 2009



Pierre GM-2 wrote:
> 
> As a workwaround, perhaps you could use np.object instead of np.str  
> while defining your array. You can then get the maximum string length  
> by looping, as David suggested, and then use .astype to transform your  
> array...
> 

I tried this:

np.rec.fromrecords([(1,'hello'),(2,'world')],dtype=[('a',np.int8),('b',np.object_)])

but I get a TypeError:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/Users/tom/<ipython console> in <module>()

/Users/tom/Library/Python/2.6/site-packages/numpy/core/records.pyc in
fromrecords(recList, dtype, shape, formats, names, titles, aligned,
byteorder)
    625     res = retval.view(recarray)
    626 
--> 627     res.dtype = sb.dtype((record, res.dtype))
    628     return res
    629 

/Users/tom/Library/Python/2.6/site-packages/numpy/core/records.pyc in
__setattr__(self, attr, val)
    432             if attr not in fielddict:
    433                 exctype, value = sys.exc_info()[:2]
--> 434                 raise exctype, value
    435         else:
    436             fielddict =
ndarray.__getattribute__(self,'dtype').fields or {}

TypeError: Cannot change data-type for object array.

Is this a bug?

Thanks,

Thomas
-- 
View this message in context: http://old.nabble.com/Automatic-string-length-in-recarray-tp26174810p26199762.html
Sent from the Numpy-discussion mailing list archive at Nabble.com.



More information about the NumPy-Discussion mailing list