@@ -468,14 +468,17 @@ def wrapper(self, *args, **kwargs):
468
468
# gracefully handle recursive calls, when object arrays contain themselves
469
469
@_recursive_guard ()
470
470
def _array2string (a , options , separator = ' ' , prefix = "" ):
471
- # The formatter __init__s cannot deal with subclasses yet
472
- data = asarray (a )
471
+ # The formatter __init__s in _get_format_function cannot deal with
472
+ # subclasses yet, and we also need to avoid recursion issues in
473
+ # _formatArray with subclasses which return 0d arrays in place of scalars
474
+ a = asarray (a )
473
475
474
476
if a .size > options ['threshold' ]:
475
477
summary_insert = "..."
476
- data = _leading_trailing (data , options ['edgeitems' ])
478
+ data = _leading_trailing (a , options ['edgeitems' ])
477
479
else :
478
480
summary_insert = ""
481
+ data = a
479
482
480
483
# find the right formatting function for the array
481
484
format_function = _get_format_function (data , ** options )
@@ -501,7 +504,7 @@ def array2string(a, max_line_width=None, precision=None,
501
504
502
505
Parameters
503
506
----------
504
- a : ndarray
507
+ a : array_like
505
508
Input array.
506
509
max_line_width : int, optional
507
510
The maximum number of columns the string should span. Newline
@@ -763,7 +766,7 @@ def recurser(index, hanging_indent, curr_width):
763
766
764
767
if show_summary :
765
768
if legacy == '1.13' :
766
- # trailing space, fixed number of newlines, and fixed separator
769
+ # trailing space, fixed nbr of newlines, and fixed separator
767
770
s += hanging_indent + summary_insert + ", \n "
768
771
else :
769
772
s += hanging_indent + summary_insert + line_sep
@@ -1413,6 +1416,8 @@ def array_repr(arr, max_line_width=None, precision=None, suppress_small=None):
1413
1416
1414
1417
return arr_str + spacer + dtype_str
1415
1418
1419
+ _guarded_str = _recursive_guard ()(str )
1420
+
1416
1421
def array_str (a , max_line_width = None , precision = None , suppress_small = None ):
1417
1422
"""
1418
1423
Return a string representation of the data in an array.
@@ -1455,7 +1460,10 @@ def array_str(a, max_line_width=None, precision=None, suppress_small=None):
1455
1460
# so floats are not truncated by `precision`, and strings are not wrapped
1456
1461
# in quotes. So we return the str of the scalar value.
1457
1462
if a .shape == ():
1458
- return str (a [()])
1463
+ # obtain a scalar and call str on it, avoiding problems for subclasses
1464
+ # for which indexing with () returns a 0d instead of a scalar by using
1465
+ # ndarray's getindex. Also guard against recursive 0d object arrays.
1466
+ return _guarded_str (np .ndarray .__getitem__ (a , ()))
1459
1467
1460
1468
return array2string (a , max_line_width , precision , suppress_small , ' ' , "" )
1461
1469
0 commit comments