numpy AxisError: axis 1 is out of bounds for array of dimension 1 (tested)

numpy.exceptions.AxisError: axis 1 is out of bounds for array of dimension 1 means you passed axis=1 to a 1-D array, which only has axis 0. Also: np.AxisError no longer imports on NumPy 2; use np.exceptions.AxisError. Tested on NumPy 1.26.4 and 2.5.4.

1. The error

import numpy as np

a = np.array([1, 2, 3])
for call in (lambda: a.sum(axis=1), lambda: np.concatenate([a, a], axis=1),
             lambda: np.ones((2, 3)).mean(axis=2), lambda: np.expand_dims(a, 3)):
    try:
        call()
    except Exception as e:
        print(type(e).__module__ + "." + type(e).__name__ + ":", e)

Output (identical on NumPy 1.26.4 and 2.5.4):

numpy.exceptions.AxisError: axis 1 is out of bounds for array of dimension 1
numpy.exceptions.AxisError: axis 1 is out of bounds for array of dimension 1
numpy.exceptions.AxisError: axis 2 is out of bounds for array of dimension 2
numpy.exceptions.AxisError: axis 3 is out of bounds for array of dimension 2

Axes are numbered from 0, so an array with ndim == 1 has only axis 0 (or -1). axis=1 on a 1-D array is the most common case: code written for a 2-D table receives a single row or column. expand_dims counts the new dimension, so its limit is ndim (the message reports dimension 2 for a 1-D input).

2. np.AxisError vs np.exceptions.AxisError (NumPy 2)

import numpy as np

print(issubclass(np.exceptions.AxisError, (ValueError, IndexError)))
try:
    from numpy import AxisError
    print("np.AxisError OK")
except ImportError as e:
    print("ImportError:", e)
try:
    np.array([1, 2]).sum(axis=1)
except np.exceptions.AxisError as e:
    print("caught:", e)

NumPy 1.26.4:

True
np.AxisError OK
caught: axis 1 is out of bounds for array of dimension 1

NumPy 2.5.4:

True
ImportError: cannot import name 'AxisError' from 'numpy' (/usr/local/lib/python3.14/site-packages/numpy/__init__.py)
caught: axis 1 is out of bounds for array of dimension 1

NumPy 2 removed np.AxisError from the main namespace; use np.exceptions.AxisError, which already exists on 1.26.4, so it works on both. It subclasses both ValueError and IndexError, so except ValueError also catches it.

3. Fixes

import numpy as np

a = np.array([1, 2, 3])
print(a.ndim, a.shape, a.sum(axis=0), a.sum(axis=-1))
print(np.stack([a, a], axis=1))          # new axis: shape (3, 2)
print(np.concatenate([a[:, None], a[:, None]], axis=1).shape)
print(np.atleast_2d(a).sum(axis=1))

Output (identical on NumPy 1.26.4 and 2.5.4):

1 (3,) 6 6
[[1 1]
 [2 2]
 [3 3]]
(3, 2)
[6]

Check a.ndim and a.shape first. On a 1-D array use axis=0 or axis=-1 (the last axis, which works for any dimension). To put 1-D arrays side by side as columns, use np.stack(..., axis=1), which creates the new axis, or add one with [:, None] before concatenate. np.atleast_2d makes code that expects 2-D input accept a single row.

4. pandas Series

import numpy as np
import pandas as pd

s = pd.Series([1, 2, 3])
try:
    np.sum(s.to_numpy(), axis=1)
except Exception as e:
    print(type(e).__name__ + ":", e)
try:
    s.sum(axis=1)
except Exception as e:
    print(type(e).__name__ + ":", e)
print(pd.DataFrame({"a": [1, 2], "b": [3, 4]}).sum(axis=1).tolist())

Output (identical on NumPy 1.26.4 and 2.5.4):

AxisError: axis 1 is out of bounds for array of dimension 1
ValueError: No axis named 1 for object type Series
[4, 6]

A Series is 1-D: NumPy raises AxisError, while pandas itself says "No axis named 1 for object type Series" (a ValueError; tested with pandas 3.0.6 and 3.1.0). Row-wise sums need a DataFrame.

Related: operands could not be broadcast together · numpy has no attribute 'int' · NumPy RuntimeWarning.

Frequently asked questions

What does 'axis 1 is out of bounds for array of dimension 1' mean?

The array is 1-D, so the only valid axes are 0 and -1. Check a.ndim; use axis=0 or -1, or reshape to 2-D first.

Why does 'from numpy import AxisError' fail?

NumPy 2 removed it from the main namespace. Use numpy.exceptions.AxisError, which also exists on NumPy 1.26.

How do I combine 1-D arrays as columns without AxisError?

np.stack([a, b], axis=1); concatenate needs existing 2-D arrays, e.g. a[:, None].

Written with AI assistance. Measured on 2026-10-23 in Docker: python:3.11-alpine with numpy 1.26.4 and pandas 3.0.6, and python:3-alpine (Python 3.14.7) with numpy 2.5.4 and pandas 3.1.0. Each block was run verbatim and the output pasted unedited. PlainTables is our extension; Python, NumPy and pandas are free.