Skip to content

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

@carlosgmartin

Description

@carlosgmartin

Proposed new feature or change:

Mailing list post

It is common to want to pad an array along a specific axis. Examples:

Doing so with numpy.pad 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 cumbersome 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:

# 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions