Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2530d2d

Browse filesBrowse files
committed
ENH: include shape in repr also for summarized arrays
So that it is always there when the shape cannot be inferred from list
1 parent 0d3f29b commit 2530d2d
Copy full SHA for 2530d2d

File tree

Expand file treeCollapse file tree

2 files changed

+56
-38
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+56
-38
lines changed

‎numpy/_core/arrayprint.py

Copy file name to clipboardExpand all lines: numpy/_core/arrayprint.py
+13-4Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
@@ -1586,11 +1591,15 @@ def _array_repr_implementation(
15861591
lst = array2string(arr, max_line_width, precision, suppress_small,
15871592
', ', prefix, suffix=")")
15881593

1594+
# Add dtype and shape information if these cannot be inferred from
1595+
# the array string.
15891596
extras = []
15901597
if not dtype_is_implied(arr.dtype) or arr.size == 0:
15911598
extras.append(f"dtype={dtype_short_repr(arr.dtype)}")
1592-
if arr.size == 0 and arr.shape != (0,):
1593-
extras.append(f"{shape=}")
1599+
if (arr.size == 0 and arr.shape != (0,)
1600+
or current_options['legacy'] > 210
1601+
and arr.size > current_options['threshold']):
1602+
extras.append(f"shape={arr.shape}")
15941603

15951604
if not extras:
15961605
return prefix + lst + ")"

‎numpy/_core/tests/test_arrayprint.py

Copy file name to clipboardExpand all lines: numpy/_core/tests/test_arrayprint.py
+43-34Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,13 @@ def test_summarize_1d(self):
346346
assert_equal(str(A), strA)
347347

348348
reprA = 'array([ 0, 1, 2, ..., 998, 999, 1000])'
349-
assert_equal(repr(A), reprA)
349+
try:
350+
np.set_printoptions(legacy='2.1')
351+
assert_equal(repr(A), reprA)
352+
finally:
353+
np.set_printoptions(legacy=False)
354+
355+
assert_equal(repr(A), reprA.replace(')', ', shape=(1001,))'))
350356

351357
def test_summarize_2d(self):
352358
A = np.arange(1002).reshape(2, 501)
@@ -356,7 +362,13 @@ def test_summarize_2d(self):
356362

357363
reprA = 'array([[ 0, 1, 2, ..., 498, 499, 500],\n' \
358364
' [ 501, 502, 503, ..., 999, 1000, 1001]])'
359-
assert_equal(repr(A), reprA)
365+
try:
366+
np.set_printoptions(legacy='2.1')
367+
assert_equal(repr(A), reprA)
368+
finally:
369+
np.set_printoptions(legacy=False)
370+
371+
assert_equal(repr(A), reprA.replace(')', ', shape=(2, 501))'))
360372

361373
def test_summarize_structure(self):
362374
A = (np.arange(2002, dtype="<i8").reshape(2, 1001)
@@ -1040,7 +1052,7 @@ def test_edgeitems(self):
10401052
10411053
[[18, ..., 20],
10421054
...,
1043-
[24, ..., 26]]])""")
1055+
[24, ..., 26]]], shape=(3, 3, 3))""")
10441056
)
10451057

10461058
b = np.zeros((3, 3, 1, 1))
@@ -1061,40 +1073,37 @@ def test_edgeitems(self):
10611073
10621074
...,
10631075
1064-
[[0.]]]])""")
1076+
[[0.]]]], shape=(3, 3, 1, 1))""")
10651077
)
10661078

10671079
# 1.13 had extra trailing spaces, and was missing newlines
1068-
np.set_printoptions(legacy='1.13')
1069-
1070-
assert_equal(
1071-
repr(a),
1072-
textwrap.dedent("""\
1073-
array([[[ 0, ..., 2],
1074-
...,
1075-
[ 6, ..., 8]],
1076-
1077-
...,
1078-
[[18, ..., 20],
1079-
...,
1080-
[24, ..., 26]]])""")
1081-
)
1082-
1083-
assert_equal(
1084-
repr(b),
1085-
textwrap.dedent("""\
1086-
array([[[[ 0.]],
1087-
1088-
...,
1089-
[[ 0.]]],
1090-
1091-
1092-
...,
1093-
[[[ 0.]],
1094-
1095-
...,
1096-
[[ 0.]]]])""")
1097-
)
1080+
try:
1081+
np.set_printoptions(legacy='1.13')
1082+
assert_equal(repr(a), (
1083+
"array([[[ 0, ..., 2],\n"
1084+
" ..., \n"
1085+
" [ 6, ..., 8]],\n"
1086+
"\n"
1087+
" ..., \n"
1088+
" [[18, ..., 20],\n"
1089+
" ..., \n"
1090+
" [24, ..., 26]]])")
1091+
)
1092+
assert_equal(repr(b), (
1093+
"array([[[[ 0.]],\n"
1094+
"\n"
1095+
" ..., \n"
1096+
" [[ 0.]]],\n"
1097+
"\n"
1098+
"\n"
1099+
" ..., \n"
1100+
" [[[ 0.]],\n"
1101+
"\n"
1102+
" ..., \n"
1103+
" [[ 0.]]]])")
1104+
)
1105+
finally:
1106+
np.set_printoptions(legacy=False)
10981107

10991108
def test_edgeitems_structured(self):
11001109
np.set_printoptions(edgeitems=1, threshold=1)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.