summaryrefslogtreecommitdiff
path: root/numpy/lib/arraypad.py
diff options
context:
space:
mode:
Diffstat (limited to 'numpy/lib/arraypad.py')
-rw-r--r--numpy/lib/arraypad.py30
1 files changed, 22 insertions, 8 deletions
diff --git a/numpy/lib/arraypad.py b/numpy/lib/arraypad.py
index 2dad99c34..b8966e543 100644
--- a/numpy/lib/arraypad.py
+++ b/numpy/lib/arraypad.py
@@ -1208,7 +1208,7 @@ def pad(array, pad_width, mode, **kwargs):
length to the vector argument with padded values replaced. It has the
following signature::
- padding_func(vector, iaxis_pad_width, iaxis, **kwargs)
+ padding_func(vector, iaxis_pad_width, iaxis, kwargs)
where
@@ -1222,7 +1222,7 @@ def pad(array, pad_width, mode, **kwargs):
the end of vector.
iaxis : int
The axis currently being calculated.
- kwargs : misc
+ kwargs : dict
Any keyword arguments the function requires.
Examples
@@ -1272,21 +1272,27 @@ def pad(array, pad_width, mode, **kwargs):
>>> np.lib.pad(a, (2, 3), 'wrap')
array([4, 5, 1, 2, 3, 4, 5, 1, 2, 3])
- >>> def padwithtens(vector, pad_width, iaxis, kwargs):
- ... vector[:pad_width[0]] = 10
- ... vector[-pad_width[1]:] = 10
+ >>> def pad_with(vector, pad_width, iaxis, kwargs):
+ ... pad_value = kwargs.get('padder', 10)
+ ... vector[:pad_width[0]] = pad_value
+ ... vector[-pad_width[1]:] = pad_value
... return vector
-
>>> a = np.arange(6)
>>> a = a.reshape((2, 3))
-
- >>> np.lib.pad(a, 2, padwithtens)
+ >>> np.lib.pad(a, 2, pad_with)
array([[10, 10, 10, 10, 10, 10, 10],
[10, 10, 10, 10, 10, 10, 10],
[10, 10, 0, 1, 2, 10, 10],
[10, 10, 3, 4, 5, 10, 10],
[10, 10, 10, 10, 10, 10, 10],
[10, 10, 10, 10, 10, 10, 10]])
+ >>> np.lib.pad(a, 2, pad_with, padder=100)
+ array([[100, 100, 100, 100, 100, 100, 100],
+ [100, 100, 100, 100, 100, 100, 100],
+ [100, 100, 0, 1, 2, 100, 100],
+ [100, 100, 3, 4, 5, 100, 100],
+ [100, 100, 100, 100, 100, 100, 100],
+ [100, 100, 100, 100, 100, 100, 100]])
"""
if not np.asarray(pad_width).dtype.kind == 'i':
raise TypeError('`pad_width` must be of integral type.')
@@ -1407,6 +1413,14 @@ def pad(array, pad_width, mode, **kwargs):
elif mode == 'reflect':
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.