8000 BUG: fix incorrect printing of 1D masked arrays · charris/numpy@7ce6556 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7ce6556

Browse files
saimncharris
authored andcommitted
BUG: fix incorrect printing of 1D masked arrays
Ref numpy#7621. numpy#6748 added `np.ma.MaskedArray._print_width` which is used to cut a masked array before printing it (to save memory and cpu time during the conversion to the object dtype). But this doesn't work correctly for 1D arrays, for which up to 1000 values can be printed before cutting the array. So this commit adds a new class variable `_print_width_1d` to handle the 1D case separately.
1 parent 693bead commit 7ce6556

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
@@ -2698,8 +2698,11 @@ class MaskedArray(ndarray):
26982698
_defaultmask = nomask
26992699
_defaulthardmask = False
27002700
_baseclass = ndarray
2701-
# Maximum number of elements per axis used when printing an array.
2701+
2702+
# Maximum number of elements per axis used when printing an array. The
2703+
# 1d case is handled separately because we need more values in this case.
27022704
_print_width = 100
2705+
_print_width_1d = 1500
27032706

27042707
def __new__(cls, data=None, mask=nomask, dtype=None, copy=False,
27052708
subok=True, ndmin=0, fill_value=None, keep_mask=True,
@@ -3776,9 +3779,11 @@ def __str__(self):
37763779
mask = m
37773780
# For big arrays, to avoid a costly conversion to the
37783781
# object dtype, extract the corners before the conversion.
3782+
print_width = (self._print_width if self.ndim > 1
3783+
else self._print_width_1d)
37793784
for axis in range(self.ndim):
3780-
if data.shape[axis] > self._print_width:
3781-
ind = self._print_width // 2
3785+
if data.shape[axis] > print_width:
3786+
ind = print_width // 2
37823787
arr = np.split(data, (ind, -ind), axis=axis)
37833788
data = np.concatenate((arr[0], arr[2]), axis=axis)
37843789
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