@@ -83,12 +83,14 @@ def _make_options_dict(precision=None, threshold=None, edgeitems=None,
8383 options ['legacy' ] = 121
8484 elif legacy == '1.25' :
8585 options ['legacy' ] = 125
86+ elif legacy == '2.1' :
87+ options ['legacy' ] = 201
8688 elif legacy is None :
8789 pass # OK, do nothing.
8890 else :
8991 warnings .warn (
9092 "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 )
9294
9395 if threshold is not None :
9496 # forbid the bad threshold arg suggested by stack overflow, gh-12351
@@ -214,13 +216,16 @@ def set_printoptions(precision=None, threshold=None, edgeitems=None,
214216 that numeric scalars are printed without their type information, e.g.
215217 as ``3.0`` rather than ``np.float64(3.0)``.
216218
219+ If set to ``'2.1'``, shape information is not given when arrays are
220+ summarized (i.e., multiple elements replaced with ``...``).
221+
217222 If set to `False`, disables legacy mode.
218223
219224 Unrecognized strings will be ignored with a warning for forward
220225 compatibility.
221226
222227 .. versionchanged:: 1.22.0
223- .. versionchanged:: 2.0
228+ .. versionchanged:: 2.2
224229
225230 override_repr: callable, optional
226231 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,
249254
250255 >>> np.set_printoptions(threshold=5)
251256 >>> np.arange(10)
252- array([0, 1, 2, ..., 7, 8, 9])
257+ array([0, 1, 2, ..., 7, 8, 9], shape=(10,) )
253258
254259 Small results can be suppressed:
255260
@@ -282,7 +287,7 @@ def set_printoptions(precision=None, threshold=None, edgeitems=None,
282287
283288 >>> with np.printoptions(precision=2, suppress=True, threshold=5):
284289 ... 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,) )
286291
287292 """
288293 _set_printoptions (precision , threshold , edgeitems , linewidth , suppress ,
@@ -1578,39 +1583,41 @@ def _array_repr_implementation(
15781583 else :
15791584 class_name = "array"
15801585
1581- skipdtype = dtype_is_implied (arr .dtype ) and arr .size > 0
1582-
15831586 prefix = class_name + "("
1584- suffix = ")" if skipdtype else ","
1585-
15861587 if (current_options ['legacy' ] <= 113 and
15871588 arr .shape == () and not arr .dtype .names ):
15881589 lst = repr (arr .item ())
1589- elif arr . size > 0 or arr . shape == ( 0 ,) :
1590+ else :
15901591 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.
16041611 # Note: This line gives the correct result even when rfind returns -1.
16051612 last_line_len = len (arr_str ) - (arr_str .rfind ('\n ' ) + 1 )
16061613 spacer = " "
16071614 if current_options ['legacy' ] <= 113 :
16081615 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 )
16121619
1613- return arr_str + spacer + dtype_str
1620+ return arr_str + spacer + extra_str
16141621
16151622
16161623def _array_repr_dispatcher (
0 commit comments