summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2017-05-01 08:35:34 -0600
committerGitHub <noreply@github.com>2017-05-01 08:35:34 -0600
commit9c3d2474b391bd110ef7ade1ecc54acfe41e6f2a (patch)
treeab8ad0b4ef90334b94bacb6a3b0766405650b009
parent2187003ec4b3c81a6246b5bbb24338e25d493f7a (diff)
parent89d7d1d51337bdfad1123f18b9fdb126240cf000 (diff)
downloadnumpy-9c3d2474b391bd110ef7ade1ecc54acfe41e6f2a.tar.gz
Merge pull request #8989 from eric-wieser/object-repr
ENH: Print object arrays containing lists unambiguously
-rw-r--r--doc/release/1.13.0-notes.rst7
-rw-r--r--numpy/core/arrayprint.py11
-rw-r--r--numpy/core/tests/test_arrayprint.py8
3 files changed, 26 insertions, 0 deletions
diff --git a/doc/release/1.13.0-notes.rst b/doc/release/1.13.0-notes.rst
index f594c1825..5362dc091 100644
--- a/doc/release/1.13.0-notes.rst
+++ b/doc/release/1.13.0-notes.rst
@@ -332,6 +332,13 @@ New ``positive`` ufunc
This ufunc corresponds to unary `+`, but unlike `+` on an ndarray it will raise
an error if array values do not support numeric operations.
+Better ``repr`` of object arrays
+--------------------------------
+Object arrays that contain themselves no longer cause a recursion error.
+
+Object arrays that contain ``list`` objects are now printed in a way that makes
+clear the difference between a 2d object array, and a 1d object array of lists.
+
Changes
=======
diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py
index dba9dffb3..e54f4602a 100644
--- a/numpy/core/arrayprint.py
+++ b/numpy/core/arrayprint.py
@@ -102,6 +102,7 @@ def set_printoptions(precision=None, threshold=None, edgeitems=None,
- 'complexfloat'
- 'longcomplexfloat' : composed of two 128-bit floats
- 'numpystr' : types `numpy.string_` and `numpy.unicode_`
+ - 'object' : `np.object_` arrays
- 'str' : all other strings
Other keys that can be used to set a group of types at once are::
@@ -241,6 +242,13 @@ def _boolFormatter(x):
else:
return 'False'
+def _object_format(o):
+ """ Object arrays containing lists should be printed unambiguously """
+ if type(o) is list:
+ fmt = 'list({!r})'
+ else:
+ fmt = '{!r}'
+ return fmt.format(o)
def repr_format(x):
return repr(x)
@@ -256,6 +264,7 @@ def _get_formatdict(data, precision, suppress_small, formatter):
'longcomplexfloat': lambda: LongComplexFormat(precision),
'datetime': lambda: DatetimeFormat(data),
'timedelta': lambda: TimedeltaFormat(data),
+ 'object': lambda: _object_format,
'numpystr': lambda: repr_format,
'str': lambda: str}
@@ -326,6 +335,8 @@ def _get_format_function(data, precision, suppress_small, formatter):
return formatdict['numpystr']()
elif issubclass(dtypeobj, _nt.datetime64):
return formatdict['datetime']()
+ elif issubclass(dtypeobj, _nt.object_):
+ return formatdict['object']()
else:
return formatdict['numpystr']()
diff --git a/numpy/core/tests/test_arrayprint.py b/numpy/core/tests/test_arrayprint.py
index b228527da..e7ac0cdfd 100644
--- a/numpy/core/tests/test_arrayprint.py
+++ b/numpy/core/tests/test_arrayprint.py
@@ -52,6 +52,14 @@ class TestArrayRepr(object):
assert_equal(repr(first),
'array(array(array(..., dtype=object), dtype=object), dtype=object)')
+ def test_containing_list(self):
+ # printing square brackets directly would be ambiguuous
+ arr1d = np.array([None, None])
+ arr1d[0] = [1, 2]
+ arr1d[1] = [3]
+ assert_equal(repr(arr1d),
+ 'array([list([1, 2]), list([3])], dtype=object)')
+
class TestComplexArray(TestCase):
def test_str(self):