Inheriting from numpy.recarray, __unicode__ issue
<p>I have made a subclass of a numpy.recarray. The purpose of the class is to provide pretty printing for record arrays while maintaining the record array functionality. </p>

<p>Here is the code:</p>

<pre><code>import numpy as np
import re

class TableView(np.recarray):

    def __new__(cls,array):
        return np.asarray(array).view(cls)

    def __str__(self):
        return self.__unicode__().encode("UTF-8")

    def __repr__(self):
        return self.__unicode__().encode("UTF-8")

    def __unicode__(self):

        options = np.get_printoptions()
        nrows = len(self)

        print "unicode called"

        if nrows &gt; 2*options['edgeitems']:
            if nrows &gt; options['threshold']:
                nrows = 2*options['edgeitems'] + 1

        ncols = len(self.dtype)

        fields = self.dtype.names

        strdata = np.empty((nrows + 1,ncols),dtype='S32')
        strdata[0] = fields

        np_len = np.vectorize(lambda x: len(x))
        maxcolchars = np.empty(ncols,dtype='i4')

        for i, field in enumerate(fields):
            strdata[1:,i] = re.sub('[\[\]]','',np.array_str(self[field])).split()
            maxcolchars[i] = np.amax(np_len(strdata[:,i]))

        rowformat = ' '.join(["{:&gt;%s}" % maxchars for maxchars in maxcolchars])
        formatrow = lambda row: (rowformat).format(*row)
        strdata = np.apply_along_axis(formatrow,1,strdata)

        return '\n'.join(strdata)
</code></pre>

<p>Here is how it prints:</p>

<pre><code>In [3]: x = np.array([(22, 2, -1000000000.0, 2000.0), (22, 2, 400.0, 2000.0),
   ...:  (22, 2, 500.0, 2000.0), (44, 2, 800.0, 4000.0), (55, 5, 900.0, 5000.0),
   ...:  (55, 5, 1000.0, 5000.0), (55, 5, 8900.0, 5000.0),
   ...:  (55, 5, 11400.0, 5000.0), (33, 3, 14500.0, 3000.0),
   ...:  (33, 3, 40550.0, 3000.0), (33, 3, 40990.0, 3000.0),
   ...:  (33, 3, 44400.01213545, 3000.0)],  
   ...:           dtype=[('subcase', '&lt;i4'), ('id', '&lt;i4'), ('vonmises', '&lt;f4'), ('maxprincipal', '&lt;f4')])

In [6]: TableView(x)
unicode called
Out[6]: 
subcase id        vonmises maxprincipal
     22  2 -1.00000000e+09        2000.
     22  2  4.00000000e+02        2000.
     22  2  5.00000000e+02        2000.
     44  2  8.00000000e+02        4000.
     55  5  9.00000000e+02        5000.
     55  5  1.00000000e+03        5000.
     55  5  8.90000000e+03        5000.
     55  5  1.14000000e+04        5000.
     33  3  1.45000000e+04        3000.
     33  3  4.05500000e+04        3000.
     33  3  4.09900000e+04        3000.
     33  3  4.44000117e+04        3000.
</code></pre>

<p>But this does not work when i print only one row:</p>

<pre><code>In [7]: TableView(x)[0]
Out[7]: (22, 2, -1000000000.0, 2000.0)
</code></pre>

<p>It works for multiple rows:</p>

<pre><code>In [8]: TableView(x)[0:1]
unicode called
Out[8]: 
subcase id        vonmises maxprincipal
     22  2 -1.00000000e+09        2000.
</code></pre>

<p>Upon further investigation:</p>

<pre><code>In [10]: type(TableView(x)[0])
Out[10]: numpy.record

In [11]: type(TableView(x)[0:1])
Out[11]: __main__.TableView
</code></pre>

<p>How can i make a numpy.record of TableView have the same <strong>unicode</strong>?</p>

<p>Your diagnosis is right.  A single element of this class is a <code>record</code>, not an <code>Tableview</code> array.  </p>

<p>And indexing with a slice or list, <code>[0:1]</code> or <code>[[0]]</code>, is the immediate solution.</p>

<p>Trying to subclass <code>np.record</code> and changing the elements of the <code>Tableview</code> seems complicated.</p>

<p>You might try customizing the <code>__getitem__</code> method.  This is called when you index the array.</p>

<p>It ends with:</p>

<pre><code>    else:
        # return a single element
        return obj
</code></pre>

<p>A modified version might return a single element Tableview instead</p>

<pre><code>        return Tableview([obj])
</code></pre>

<p>But that might produce some sort of endless recursion, keeping you from accessing elements as regular records.</p>

<p>Otherwise you might just want to live with the slice indexing.  </p>

python numpy recarray