numpy.lib.array_utils.normalize_axis_index#

lib.array_utils.normalize_axis_index(axis, ndim, msg_prefix=None)#

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.

New in version 1.13.0.

Parameters:
axisint

The un-normalized index of the axis. Can be negative

ndimint

The number of dimensions of the array that axis should be normalized against

msg_prefixstr

A prefix to put before the message, typically the name of the argument

Returns:
normalized_axisint

The normalized axis index, such that 0 <= normalized_axis < ndim

Raises:
AxisError

If the axis index is invalid, when -ndim <= axis < ndim is false.

Examples

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