From 51ef0c409e20490829218c4d549a3bd4c9b073a2 Mon Sep 17 00:00:00 2001 From: Lars G Date: Wed, 2 May 2018 12:54:08 +0200 Subject: BUG: Fix padding with large integers The old way of creating the padded array padded with wrong values for large integers because the new prepended / appended array was implicitly created with dtype float64: >>> (np.zeros(1) + (2 ** 64 - 1)).astype(np.uint64) array([0], np.uint64) >>> (np.zeros(1) + (2 ** 63 - 1)).astype(np.int64) array([-9223372036854775808]) --- numpy/lib/arraypad.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'numpy/lib/arraypad.py') diff --git a/numpy/lib/arraypad.py b/numpy/lib/arraypad.py index daaa68d06..9f33753b7 100644 --- a/numpy/lib/arraypad.py +++ b/numpy/lib/arraypad.py @@ -104,8 +104,8 @@ def _prepend_const(arr, pad_amt, val, axis=-1): return np.concatenate((np.zeros(padshape, dtype=arr.dtype), arr), axis=axis) else: - return np.concatenate(((np.zeros(padshape) + val).astype(arr.dtype), - arr), axis=axis) + return np.concatenate((np.full(padshape, val, dtype=arr.dtype), arr), + axis=axis) def _append_const(arr, pad_amt, val, axis=-1): @@ -138,8 +138,8 @@ def _append_const(arr, pad_amt, val, axis=-1): return np.concatenate((arr, np.zeros(padshape, dtype=arr.dtype)), axis=axis) else: - return np.concatenate( - (arr, (np.zeros(padshape) + val).astype(arr.dtype)), axis=axis) + return np.concatenate((arr, np.full(padshape, val, dtype=arr.dtype)), + axis=axis) def _prepend_edge(arr, pad_amt, axis=-1): -- cgit v1.2.1 From a5f94a90adc15a4a5fb417a1fb5c6d4338b899a0 Mon Sep 17 00:00:00 2001 From: Lars G Date: Wed, 2 May 2018 17:39:24 +0200 Subject: MAINT: Simplify workflow in _append_const and _prepend_const --- numpy/lib/arraypad.py | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) (limited to 'numpy/lib/arraypad.py') diff --git a/numpy/lib/arraypad.py b/numpy/lib/arraypad.py index 9f33753b7..97ba12348 100644 --- a/numpy/lib/arraypad.py +++ b/numpy/lib/arraypad.py @@ -100,12 +100,8 @@ def _prepend_const(arr, pad_amt, val, axis=-1): return arr padshape = tuple(x if i != axis else pad_amt for (i, x) in enumerate(arr.shape)) - if val == 0: - return np.concatenate((np.zeros(padshape, dtype=arr.dtype), arr), - axis=axis) - else: - return np.concatenate((np.full(padshape, val, dtype=arr.dtype), arr), - axis=axis) + return np.concatenate((np.full(padshape, val, dtype=arr.dtype), arr), + axis=axis) def _append_const(arr, pad_amt, val, axis=-1): @@ -134,12 +130,8 @@ def _append_const(arr, pad_amt, val, axis=-1): return arr padshape = tuple(x if i != axis else pad_amt for (i, x) in enumerate(arr.shape)) - if val == 0: - return np.concatenate((arr, np.zeros(padshape, dtype=arr.dtype)), - axis=axis) - else: - return np.concatenate((arr, np.full(padshape, val, dtype=arr.dtype)), - axis=axis) + return np.concatenate((arr, np.full(padshape, val, dtype=arr.dtype)), + axis=axis) def _prepend_edge(arr, pad_amt, axis=-1): -- cgit v1.2.1