diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2017-01-17 10:56:19 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-17 10:56:19 -0700 |
commit | 6f378b36f16d1667704234a2f6287588a04e210b (patch) | |
tree | 3d96ea5fbb6d44ff5905b0454ed2b372c008373f /numpy | |
parent | 50956653118ec062ea429d423816e6cbfe10f24b (diff) | |
parent | c79e40ae361451b47682ea20c67c5db92e7bf63c (diff) | |
download | numpy-6f378b36f16d1667704234a2f6287588a04e210b.tar.gz |
Merge pull request #8488 from eric-wieser/recarray-repr
ENH: Improve the alignment of recarray.__repr__
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/records.py | 32 |
1 files changed, 17 insertions, 15 deletions
diff --git a/numpy/core/records.py b/numpy/core/records.py index 3bee394cd..09511c5e6 100644 --- a/numpy/core/records.py +++ b/numpy/core/records.py @@ -513,13 +513,8 @@ class recarray(ndarray): return obj 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),) + repr_dtype = self.dtype if (self.dtype.type is record or (not issubclass(self.dtype.type, nt.void))): # If this is a full record array (has numpy.record dtype), @@ -527,19 +522,26 @@ class recarray(ndarray): # represent it using the rec.array function. Since rec.array # converts dtype to a numpy.record for us, convert back # to non-record before printing - plain_dtype = self.dtype - if plain_dtype.type is record: - plain_dtype = sb.dtype((nt.void, plain_dtype)) - lf = '\n'+' '*len("rec.array(") - return ('rec.array(%s, %sdtype=%s)' % - (lst, lf, plain_dtype)) + if repr_dtype.type is record: + repr_dtype = sb.dtype((nt.void, repr_dtype)) + prefix = "rec.array(" + fmt = 'rec.array(%s, %sdtype=%s)' else: # otherwise represent it using np.array plus a view # This should only happen if the user is playing # strange games with dtypes. - lf = '\n'+' '*len("array(") - return ('array(%s, %sdtype=%s).view(numpy.recarray)' % - (lst, lf, str(self.dtype))) + prefix = "array(" + fmt = 'array(%s, %sdtype=%s).view(numpy.recarray)' + + # get data/shape string. logic taken from numeric.array_repr + if self.size > 0 or self.shape == (0,): + lst = sb.array2string(self, separator=', ', prefix=prefix) + else: + # show zero-length shape unless it is (0,) + lst = "[], shape=%s" % (repr(self.shape),) + + lf = '\n'+' '*len(prefix) + return fmt % (lst, lf, repr_dtype) def field(self, attr, val=None): if isinstance(attr, int): |