diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2015-01-30 18:29:52 -0500 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2015-01-30 18:29:52 -0500 |
commit | ea5202880e2e1876d9baa2a65e7987c99be0459b (patch) | |
tree | 994bb3a0b79dec259dee17af1df65c3a942cbf41 /numpy/core/records.py | |
parent | 456318e027871460bd737f3bd7a3d064643e50e6 (diff) | |
parent | 73a74e9e9515ad76d652e998fc1e88074e8cd820 (diff) | |
download | numpy-ea5202880e2e1876d9baa2a65e7987c99be0459b.tar.gz |
Merge pull request #5523 from ahaldane/recarray_fixrepr
BUG: recarray __repr__ gives inaccurate representation
Diffstat (limited to 'numpy/core/records.py')
-rw-r--r-- | numpy/core/records.py | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/numpy/core/records.py b/numpy/core/records.py index a31076ad6..243645436 100644 --- a/numpy/core/records.py +++ b/numpy/core/records.py @@ -485,9 +485,28 @@ class recarray(ndarray): # return a single element return obj - def __repr__(self) : - ret = ndarray.__repr__(self) - return ret.replace("recarray", "rec.array", 1) + def __repr__(self): + # get data/shape string. logic taken from numeric.array_repr + if self.size > 0 or self.shape==(0,): + lst = sb.array2string(self, separator=', ') + else: + # show zero-length shape unless it is (0,) + lst = "[], shape=%s" % (repr(self.shape),) + + if self.dtype.type is record: + # If this is a full record array (has numpy.record dtype), + # represent it using the rec.array function. Since rec.array + # converts dtype to a numpy.record for us, use only dtype.descr, + # not repr(dtype). + lf = '\n'+' '*len("rec.array(") + return ('rec.array(%s, %sdtype=%s)' % + (lst, lf, repr(self.dtype.descr))) + else: + # otherwise represent it using np.array plus a view + # (There is currently (v1.10) no other easy way to create it) + lf = '\n'+' '*len("array(") + return ('array(%s, %sdtype=%s).view(numpy.recarray)' % + (lst, lf, str(self.dtype))) def field(self, attr, val=None): if isinstance(attr, int): |