summaryrefslogtreecommitdiff
path: root/numpy/lib
diff options
context:
space:
mode:
authorIryna Shcherbina <ishcherb@redhat.com>2017-09-01 15:21:08 +0200
committerCharles Harris <charlesr.harris@gmail.com>2017-09-22 14:31:51 -0600
commitd57ada7c0c9900bfe8dfa139fa6419c4307ecb25 (patch)
tree23eca5b524d6371180fee3635f875bcc68b86148 /numpy/lib
parentba443cedf6e0194ab85f362f7d7ca89dca432e77 (diff)
downloadnumpy-d57ada7c0c9900bfe8dfa139fa6419c4307ecb25.tar.gz
BUG: fix padding an empty array in reflect mode.
Check that axes with non-zero padding are non-empty.
Diffstat (limited to 'numpy/lib')
-rw-r--r--numpy/lib/arraypad.py11
-rw-r--r--numpy/lib/tests/test_arraypad.py7
2 files changed, 15 insertions, 3 deletions
diff --git a/numpy/lib/arraypad.py b/numpy/lib/arraypad.py
index 294a68950..842f3a9fe 100644
--- a/numpy/lib/arraypad.py
+++ b/numpy/lib/arraypad.py
@@ -1406,10 +1406,15 @@ def pad(array, pad_width, mode, **kwargs):
newmat = _append_min(newmat, pad_after, chunk_after, axis)
elif mode == 'reflect':
- if narray.size == 0:
- raise ValueError("There aren't any elements to reflect in `array`")
-
for axis, (pad_before, pad_after) in enumerate(pad_width):
+ if narray.shape[axis] == 0:
+ # Axes with non-zero padding cannot be empty.
+ if pad_before > 0 or pad_after > 0:
+ raise ValueError("There aren't any elements to reflect"
+ " in axis {} of `array`".format(axis))
+ # Skip zero padding on empty axes.
+ continue
+
# Recursive padding along any axis where `pad_amt` is too large
# for indexing tricks. We can only safely pad the original axis
# length, to keep the period of the reflections consistent.
diff --git a/numpy/lib/tests/test_arraypad.py b/numpy/lib/tests/test_arraypad.py
index 0f71d3202..89653ac0c 100644
--- a/numpy/lib/tests/test_arraypad.py
+++ b/numpy/lib/tests/test_arraypad.py
@@ -640,6 +640,11 @@ class TestReflect(TestCase):
b = np.array([1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3])
assert_array_equal(a, b)
+ def test_check_padding_an_empty_array(self):
+ a = pad(np.zeros((0, 3)), ((0,), (1,)), mode='reflect')
+ b = np.zeros((0, 5))
+ assert_array_equal(a, b)
+
class TestSymmetric(TestCase):
def test_check_simple(self):
@@ -1017,6 +1022,8 @@ class ValueError1(TestCase):
def test_check_empty_array(self):
assert_raises(ValueError, pad, [], 4, mode='reflect')
assert_raises(ValueError, pad, np.ndarray(0), 4, mode='reflect')
+ assert_raises(ValueError, pad, np.zeros((0, 3)), ((1,), (0,)),
+ mode='reflect')
class ValueError2(TestCase):