@@ -83,12 +83,14 @@ def _make_options_dict(precision=None, threshold=None, edgeitems=None,
83
83
options ['legacy' ] = 121
84
84
elif legacy == '1.25' :
85
85
options ['legacy' ] = 125
86
+ elif legacy == '2.1' :
87
+ options ['legacy' ] = 201
86
88
elif legacy is None :
87
89
pass # OK, do nothing.
88
90
else :
89
91
warnings .warn (
90
92
"legacy printing option can currently only be '1.13', '1.21', "
91
- "'1.25', or `False`" , stacklevel = 3 )
93
+ "'1.25', '2.1, or `False`" , stacklevel = 3 )
92
94
93
95
if threshold is not None :
94
96
# forbid the bad threshold arg suggested by stack overflow, gh-12351
@@ -214,13 +216,16 @@ def set_printoptions(precision=None, threshold=None, edgeitems=None,
214
216
that numeric scalars are printed without their type information, e.g.
215
217
as ``3.0`` rather than ``np.float64(3.0)``.
216
218
219
+ If set to ``'2.1'``, shape information is not given when arrays are
220
+ summarized (i.e., multiple elements replaced with ``...``).
221
+
217
222
If set to `False`, disables legacy mode.
218
223
219
224
Unrecognized strings will be ignored with a warning for forward
220
225
compatibility.
221
226
222
227
.. versionchanged:: 1.22.0
223
- .. versionchanged:: 2.0
228
+ .. versionchanged:: 2.2
224
229
225
230
override_repr: callable, optional
226
231
If set a passed function will be used for generating arrays' repr.
@@ -249,7 +254,7 @@ def set_printoptions(precision=None, threshold=None, edgeitems=None,
249
254
250
255
>>> np.set_printoptions(threshold=5)
251
256
>>> np.arange(10)
252
- array([0, 1, 2, ..., 7, 8, 9])
257
+ array([0, 1, 2, ..., 7, 8, 9], shape=(10,) )
253
258
254
259
Small results can be suppressed:
255
260
@@ -282,7 +287,7 @@ def set_printoptions(precision=None, threshold=None, edgeitems=None,
282
287
283
288
>>> with np.printoptions(precision=2, suppress=True, threshold=5):
284
289
... np.linspace(0, 10, 10)
285
- array([ 0. , 1.11, 2.22, ..., 7.78, 8.89, 10. ])
290
+ array([ 0. , 1.11, 2.22, ..., 7.78, 8.89, 10. ], shape=(10,) )
286
291
287
292
"""
288
293
_set_printoptions (precision , threshold , edgeitems , linewidth , suppress ,
@@ -1578,39 +1583,41 @@ def _array_repr_implementation(
1578
1583
else :
1579
1584
class_name = "array"
1580
1585
1581
- skipdtype = dtype_is_implied (arr .dtype ) and arr .size > 0
1582
-
1583
1586
prefix = class_name + "("
1584
- suffix = ")" if skipdtype else ","
1585
-
1586
1587
if (current_options ['legacy' ] <= 113 and
1587
1588
arr .shape == () and not arr .dtype .names ):
1588
1589
lst = repr (arr .item ())
1589
- elif arr . size > 0 or arr . shape == ( 0 ,) :
1590
+ else :
1590
1591
lst = array2string (arr , max_line_width , precision , suppress_small ,
1591
- ', ' , prefix , suffix = suffix )
1592
- else : # show zero-length shape unless it is (0,)
1593
- lst = "[], shape=%s" % (repr (arr .shape ),)
1594
-
1595
- arr_str = prefix + lst + suffix
1596
-
1597
- if skipdtype :
1598
- return arr_str
1599
-
1600
- dtype_str = "dtype={})" .format (dtype_short_repr (arr .dtype ))
1601
-
1602
- # compute whether we should put dtype on a new line: Do so if adding the
1603
- # dtype would extend the last line past max_line_width.
1592
+ ', ' , prefix , suffix = ")" )
1593
+
1594
+ # Add dtype and shape information if these cannot be inferred from
1595
+ # the array string.
1596
+ extras = []
1597
+ if (arr .size == 0 and arr .shape != (0 ,)
1598
+ or current_options ['legacy' ] > 210
1599
+ and arr .size > current_options ['threshold' ]):
1600
+ extras .append (f"shape={ arr .shape } " )
1601
+ if not dtype_is_implied (arr .dtype ) or arr .size == 0 :
1602
+ extras .append (f"dtype={ dtype_short_repr (arr .dtype )} " )
1603
+
1604
+ if not extras :
1605
+ return prefix + lst + ")"
1606
+
1607
+ arr_str = prefix + lst + ","
1608
+ extra_str = ", " .join (extras ) + ")"
1609
+ # compute whether we should put extras on a new line: Do so if adding the
1610
+ # extras would extend the last line past max_line_width.
1604
1611
# Note: This line gives the correct result even when rfind returns -1.
1605
1612
last_line_len = len (arr_str ) - (arr_str .rfind ('\n ' ) + 1 )
1606
1613
spacer = " "
1607
1614
if current_options ['legacy' ] <= 113 :
1608
1615
if issubclass (arr .dtype .type , flexible ):
1609
- spacer = '\n ' + ' ' * len (class_name + "(" )
1610
- elif last_line_len + len (dtype_str ) + 1 > max_line_width :
1611
- spacer = '\n ' + ' ' * len (class_name + "(" )
1616
+ spacer = '\n ' + ' ' * len (prefix )
1617
+ elif last_line_len + len (extra_str ) + 1 > max_line_width :
1618
+ spacer = '\n ' + ' ' * len (prefix )
1612
1619
1613
- return arr_str + spacer + dtype_str
1620
+ return arr_str + spacer + extra_str
1614
1621
1615
1622
1616
1623
def _array_repr_dispatcher (
0 commit comments