On this page
numpy.AxisError
- exceptionnumpy.AxisError(axis, ndim=None, msg_prefix=None)[source]
-
Axis supplied was invalid.
This is raised whenever an
axisparameter is specified that is larger than the number of array dimensions. For compatibility with code written against older numpy versions, which raised a mixture ofValueErrorandIndexErrorfor this situation, this exception subclasses both to ensure thatexcept ValueErrorandexcept IndexErrorstatements continue to catchAxisError.New in version 1.13.
- Parameters
-
- axisint or str
-
The out of bounds axis or a custom exception message. If an axis is provided, then
ndimshould be specified as well. - ndimint, optional
-
The number of array dimensions.
- msg_prefixstr, optional
-
A prefix for the exception message.
Examples
>>> array_1d = np.arange(10) >>> np.cumsum(array_1d, axis=1) Traceback (most recent call last): ... numpy.AxisError: axis 1 is out of bounds for array of dimension 1Negative axes are preserved:
>>> np.cumsum(array_1d, axis=-2) Traceback (most recent call last): ... numpy.AxisError: axis -2 is out of bounds for array of dimension 1The class constructor generally takes the axis and arrays’ dimensionality as arguments:
>>> print(np.AxisError(2, 1, msg_prefix='error')) error: axis 2 is out of bounds for array of dimension 1Alternatively, a custom exception message can be passed:
>>> print(np.AxisError('Custom error message')) Custom error message- Attributes
-
- axisint, optional
-
The out of bounds axis or
Noneif a custom exception message was provided. This should be the axis as passed by the user, before any normalization to resolve negative indices.New in version 1.22.
- ndimint, optional
-
The number of array dimensions or
Noneif a custom exception message was provided.New in version 1.22.
© 2005–2022 NumPy Developers
Licensed under the 3-clause BSD License.
https://numpy.org/doc/1.23/reference/generated/numpy.AxisError.html