Repository: numpy/numpy
File: numpy/lib/_arraypad_impl.py (Python)

Code before:

    >>> np.pad(a, (2, 3), 'wrap')
    array([4, 5, 1, 2, 3, 4, 5, 1, 2, 3])

    >>> 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
    >>> a = np.arange(6)
    >>> a = a.reshape((2, 3))
    >>> np.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.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]])

    >>> a = np.arange(2 * 3).reshape(2, 3)
    >>> np.pad(a, {1: (1, 2)})
    array([[0, 0, 1, 2, 0, 0],
           [0, 3, 4, 5, 0, 0]])
    >>> np.pad(a, {-1: 2})
    array([[0, 0, 0, 1, 2, 0, 0],
           [0, 0, 3, 4, 5, 0, 0]])
    >>> np.pad(a, {0: (3, 0)})
    array([[0, 0, 0],
           [0, 0, 0],
           [0, 0, 0],
           [0, 1, 2],
           [3, 4, 5]])
    >>> np.pad(a, {0: (3, 0), 1: 2})
    array([[0, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 1, 2, 0, 0],
           [0, 0, 3, 4, 5, 0, 0]])
    """
    array = np.asarray(array)
    if isinstance(pad_width, dict):
        seq = [(0, 0)] * array.ndim
        for axis, width in pad_width.items():
            match width:
                case int(both):

Review comment (suggestion):
It is confusing that `0` is both the pad value and one of the values in the original array. What about this? Or add a pad value?
```suggestion
    >>> a = np.arange(1, 7).reshape(2, 3)
```

Code after:
    array([0, 1, 1, 2, 3, 4, 5, 5, 6, 7])

    >>> np.pad(a, (2, 3), 'wrap')
    array([4, 5, 1, 2, 3, 4, 5, 1, 2, 3])

    >>> 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
    >>> a = np.arange(6)
    >>> a = a.reshape((2, 3))
    >>> np.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.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]])

    >>> a = np.arange(1, 7).reshape(2, 3)
    >>> np.pad(a, {1: (1, 2)})
    array([[0, 1, 2, 3, 0, 0],
           [0, 4, 5, 6, 0, 0]])
    >>> np.pad(a, {-1: 2})
    array([[0, 0, 1, 2, 3, 0, 0],
           [0, 0, 4, 5, 6, 0, 0]])
    >>> np.pad(a, {0: (3, 0)})
    array([[0, 0, 0],
           [0, 0, 0],
           [0, 0, 0],
           [1, 2, 3],
           [4, 5, 6]])
    >>> np.pad(a, {0: (3, 0), 1: 2})
    array([[0, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 0, 0],
           [0, 0, 1, 2, 3, 0, 0],
           [0, 0, 4, 5, 6, 0, 0]])
    """
    array = np.asarray(array)
    if isinstance(pad_width, dict):
        seq = [(0, 0)] * array.ndim
        for axis, width in pad_width.items():
            match width: