On Tue, 2025-06-24 at 21:06 +0000, Carlos Martin wrote: > 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)}) > ```
Seems like a pretty reasonable extension, I wonder how many other similar functions do we have, and does it extend to that as well? I think right now it is a bit more typical to just have an `axis=` kwarg to choose in two arguments. (Which I agree is not as a nice of an API.) So I guess that is a mildly in favor from me when it comes to accepting such a change, but I am not sure if it will be quite enough to overcome friction of adding new API. - Sebastian > > 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 -- numpy-discussion@python.org > To unsubscribe send an email to numpy-discussion-le...@python.org > https://mail.python.org/mailman3//lists/numpy-discussion.python.org > Member address: sebast...@sipsolutions.net > _______________________________________________ NumPy-Discussion mailing list -- numpy-discussion@python.org To unsubscribe send an email to numpy-discussion-le...@python.org https://mail.python.org/mailman3//lists/numpy-discussion.python.org Member address: arch...@mail-archive.com