summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Wieser <wieser.eric@gmail.com>2018-01-15 22:07:42 -0800
committerEric Wieser <wieser.eric@gmail.com>2018-01-16 23:18:21 -0800
commit7f9a49b065fc02499da1bab084eb3e2c0e87b342 (patch)
tree3ed35616a8228183070909866b9edbd43460bcf3
parent5f01e54b20e38d483e8bab31bf5f953a860fe8d3 (diff)
downloadnumpy-7f9a49b065fc02499da1bab084eb3e2c0e87b342.tar.gz
ENH: Fix repr of np.record objects to match np.void types
-rw-r--r--numpy/core/records.py8
-rw-r--r--numpy/core/tests/test_records.py17
2 files changed, 23 insertions, 2 deletions
diff --git a/numpy/core/records.py b/numpy/core/records.py
index 76783bb67..fdda21205 100644
--- a/numpy/core/records.py
+++ b/numpy/core/records.py
@@ -223,10 +223,14 @@ class record(nt.void):
__module__ = 'numpy'
def __repr__(self):
- return self.__str__()
+ if get_printoptions()['legacy'] == '1.13':
+ return self.__str__()
+ return super(record, self).__repr__()
def __str__(self):
- return str(self.item())
+ if get_printoptions()['legacy'] == '1.13':
+ return str(self.item())
+ return super(record, self).__str__()
def __getattribute__(self, attr):
if attr in ['setfield', 'getfield', 'dtype']:
diff --git a/numpy/core/tests/test_records.py b/numpy/core/tests/test_records.py
index 73cfe3570..e23e9c48c 100644
--- a/numpy/core/tests/test_records.py
+++ b/numpy/core/tests/test_records.py
@@ -4,6 +4,7 @@ import sys
import collections
import pickle
import warnings
+import textwrap
from os import path
import numpy as np
@@ -112,6 +113,22 @@ class TestFromrecords(object):
dtype=[('foo', '<i4'), ('bar', '<f8')])""")
)
+ def test_0d_recarray_repr(self):
+ # testing infered integer types is unpleasant due to sizeof(int) varying
+ arr_0d = np.rec.array((np.int32(1), 2.0, np.datetime64('2003')))
+ assert_equal(repr(arr_0d), textwrap.dedent("""\
+ rec.array((1, 2., '2003'),
+ dtype=[('f0', '<i4'), ('f1', '<f8'), ('f2', '<M8[Y]')])"""))
+
+ record = arr_0d[()]
+ assert_equal(repr(record), "(1, 2., '2003')")
+ # 1.13 converted to python scalars before the repr
+ try:
+ np.set_printoptions(legacy='1.13')
+ assert_equal(repr(record), '(1, 2.0, datetime.date(2003, 1, 1))')
+ finally:
+ np.set_printoptions(legacy=False)
+
def test_recarray_from_repr(self):
a = np.array([(1,'ABC'), (2, "DEF")],
dtype=[('foo', int), ('bar', 'S4')])