8000 Merge pull request #7658 from saimn/fix-ma-repr · numpy/numpy@59a7b25 · GitHub
[go: up one dir, main page]

Skip to content

Commit 59a7b25

Browse files
committed
Merge pull request #7658 from saimn/fix-ma-repr
BUG: fix incorrect printing of 1D masked arrays
2 parents 313a9b2 + d805e9b commit 59a7b25

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

numpy/ma/core.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2709,8 +2709,11 @@ class MaskedArray(ndarray):
27092709
_defaultmask = nomask
27102710
_defaulthardmask = False
27112711
_baseclass = ndarray
2712-
# Maximum number of elements per axis used when printing an array.
2712+
2713+
# Maximum number of elements per axis used when printing an array. The
2714+
# 1d case is handled separately because we need more values in this case.
27132715
_print_width = 100
2716+
_print_width_1d = 1500
27142717

27152718
def __new__(cls, data=None, mask=nomask, dtype=None, copy=False,
27162719
subok=True, ndmin=0, fill_value=None, keep_mask=True,
@@ -3796,9 +3799,11 @@ def __str__(self):
37963799
mask = m
37973800
# For big arrays, to avoid a costly conversion to the
37983801
# object dtype, extract the corners before the conversion.
3802+
print_width = (self._print_width if self.ndim > 1
3803+
else self._print_width_1d)
37993804
for axis in range(self.ndim):
3800-
if data.shape[axis] > self._print_width:
3801-
ind = self._print_width // 2
3805+
if data.shape[axis] > print_width:
3806+
ind = print_width // 2
38023807
arr = np.split(data, (ind, -ind), axis=axis)
38033808
data = np.concatenate((arr[0], arr[2]), axis=axis)
38043809
arr = np.split(mask, (ind, -ind), axis=axis)

numpy/ma/tests/test_core.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,15 @@ def test_str_repr(self):
451451
' mask = [False True False],\n'
452452
' fill_value = 999999)\n')
453453

454+
a = np.ma.arange(2000)
455+
a[1:50] = np.ma.masked
456+
assert_equal(
457+
repr(a),
458+
'masked_array(data = [0 -- -- ..., 1997 1998 1999],\n'
459+
' mask = [False True True ..., False False False],\n'
460+
' fill_value = 999999)\n'
461+
)
462+
454463
def test_pickling(self):
455464
# Tests pickling
456465
a = arange(10)

0 commit comments

Comments
 (0)
0