@@ -435,14 +435,17 @@ def wrapper(self, *args, **kwargs):
435
435
# gracefully handle recursive calls, when object arrays contain themselves
436
436
@_recursive_guard ()
437
437
def _array2string (a , options , separator = ' ' , prefix = "" ):
438
- # The formatter __init__s cannot deal with subclasses yet
439
- data = asarray (a )
438
+ # The formatter __init__s in _get_format_function cannot deal with
439
+ # subclasses yet, and we also need to avoid recursion issues in
440
+ # _formatArray with subclasses which return 0d arrays in place of scalars
441
+ a = asarray (a )
440
442
441
443
if a .size > options ['threshold' ]:
442
444
summary_insert = "..."
443
- data = _leading_trailing (data , options ['edgeitems' ])
445
+ data = _leading_trailing (a , options ['edgeitems' ])
444
446
else :
445
447
summary_insert = ""
448
+ data = a
446
449
447
450
# find the right formatting function for the array
448
451
format_function = _get_format_function (data , ** options )
@@ -468,7 +471,7 @@ def array2string(a, max_line_width=None, precision=None,
468
471
469
472
Parameters
470
473
----------
471
- a : ndarray
474
+ a : array_like
472
475
Input array.
473
476
max_line_width : int, optional
474
477
The maximum number of columns the string should span. Newline
@@ -730,7 +733,7 @@ def recurser(index, hanging_indent, curr_width):
730
733
731
734
if show_summary :
732
735
if legacy == '1.13' :
733
- # trailing space, fixed number of newlines, and fixed separator
736
+ # trailing space, fixed nbr of newlines, and fixed separator
734
737
s += hanging_indent + summary_insert + ", \n "
735
738
else :
736
739
s += hanging_indent + summary_insert + line_sep
@@ -1380,6 +1383,8 @@ def array_repr(arr, max_line_width=None, precision=None, suppress_small=None):
1380
1383
1381
1384
return arr_str + spacer + dtype_str
1382
1385
1386
+ _guarded_str = _recursive_guard ()(str )
1387
+
1383
1388
def array_str (a , max_line_width = None , precision = None , suppress_small = None ):
1384
1389
"""
1385
1390
Return a string representation of the data in an array.
@@ -1422,7 +1427,10 @@ def array_str(a, max_line_width=None, precision=None, suppress_small=None):
1422
1427
# so floats are not truncated by `precision`, and strings are not wrapped
1423
1428
# in quotes. So we return the str of the scalar value.
1424
1429
if a .shape == ():
1425
- return str (a [()])
1430
+ # obtain a scalar and call str on it, avoiding problems for subclasses
1431
+ # for which indexing with () returns a 0d instead of a scalar by using
1432
+ # ndarray's getindex. Also guard against recursive 0d object arrays.
1433
+ return _guarded_str (np .ndarray .__getitem__ (a , ()))
1426
1434
1427
1435
return array2string (a , max_line_width , precision , suppress_small , ' ' , "" )
1428
1436
0 commit comments