Normalizes an axis index, axis, such that is a valid positive index into the shape of array with ndim dimensions. Raises an AxisError with an appropriate message if this is not possible.
Used internally by all axis-checking logic.
The un-normalized index of the axis. Can be negative
The number of dimensions of the array that axis should be normalized against
A prefix to put before the message, typically the name of the argument
The normalized axis index, such that 0 <= normalized_axis < ndim
If the axis index is invalid, when -ndim <= axis < ndim is false.
>>> import numpy as np >>> from numpy.lib.array_utils import normalize_axis_index >>> normalize_axis_index(0, ndim=3) 0 >>> normalize_axis_index(1, ndim=3) 1 >>> normalize_axis_index(-1, ndim=3) 2
>>> normalize_axis_index(3, ndim=3) Traceback (most recent call last): ... numpy.exceptions.AxisError: axis 3 is out of bounds for array ... >>> normalize_axis_index(-4, ndim=3, msg_prefix='axes_arg') Traceback (most recent call last): ... numpy.exceptions.AxisError: axes_arg: axis -4 is out of bounds ...
© 2005–2024 NumPy Developers
Licensed under the 3-clause BSD License.
https://numpy.org/doc/2.4/reference/generated/numpy.lib.array_utils.normalize_axis_index.html