diff options
Diffstat (limited to 'numpy/core')
-rw-r--r-- | numpy/core/arrayprint.py | 10 | ||||
-rw-r--r-- | numpy/core/tests/test_arrayprint.py | 8 |
2 files changed, 18 insertions, 0 deletions
diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py index dba9dffb3..f6f610f79 100644 --- a/numpy/core/arrayprint.py +++ b/numpy/core/arrayprint.py @@ -241,6 +241,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 +263,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 +334,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): |