ENH: More user-friendly way to pad a specific axis

"Carlos Martin" <[email protected]>
Newsgroups gmane.comp.python.numeric.general
Message-ID <[email protected]>
It is common to want to pad an array along a *specific* axis. Examples:

- https://stackoverflow.com/questions/72106542/how-to-pad-the-i-j-axes-of-a-3d-np-array-without-padding-its-k-axis
- https://stackoverflow.com/questions/56076094/zero-pad-ndarray-along-axis
- https://stackoverflow.com/questions/66225047/padding-a-3-dimensional-numpy-array-with-the-medians-alongside-specific-axis
- https://stackoverflow.com/questions/19349410/how-to-pad-with-zeros-a-tensor-along-some-axis-python
- https://stackoverflow.com/questions/74966041/how-to-pad-a-specific-dimension-of-a-numpy-array

Doing so with [`numpy.pad`](https://numpy.org/doc/stable/reference/generated/numpy.pad.html) requires constructing a list of pairs of length equal to the ndim of the array, with exactly one of those pairs at the right position containing the desired pad widths. This can be verbose and clumsy when there are several axes.

I propose a new, more user-friendly way to pad along a specific axis (or axes): Let the `pad_width` argument accept a *dictionary* whose keys are axes and whose values are the `(before, after)` pair (or perhaps single number) for the corresponding axis. Example:

```python3
# before
np.pad(array, [(0, 0), (0, 0), (1, 2), (0, 0), (0, 0)])
# after
np.pad(array, {-3: (1, 2)})
```

This should require only minor modification to the implementation of `numpy.pad`. If others like this idea, I can create a PR for it.
_______________________________________________
NumPy-Discussion mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/numpy-discussion.python.org
Member address: [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.