NumPy 2.0 rolled out: First major release since 2006 comes with many breaking changes

NumPy 2.0 rolled out: First major release since 2006 comes with many breaking changes

NumPy 2.0 is out, the first major release since 2006 of this fundamental mathematical library for Python, with new features and performance improvements but also breaking changes.

Perhaps the main reason this is a major release is that the team has taken the opportunity to clean up the NumPy APIs, which should make NumPy easier to learn and use. The resulting large number of breaking changes would not be acceptable in a minor release. In most cases there will be clear error messages, and there are also changes in behaviour where this was not possible.

The new version has an improved Python API with clearly distinguished public and private APIs, a new module structure, and many “non-recommended” functions and aliases removed. According to the release notes, the number of objects in the main namespace has reduced by 10 percent, and the number of objects in numpy.lib by around 80 percent. Many “outdated functions and macros” have also been removed from the API for the C language. These are listed in the notes.

On Windows, the default NumPy integer type was previously equivalent to the C long type, which is 32-bit even on 64-bit systems, for compatibility reasons. In NumPy 2.0, the default integer is now 64-bit on all 64-bit systems, including Windows.

The NumPy team warns that “it is likely that downstream packages and end user code needs to be adapted,” especially as the NumPy ABI (Application Binary Interface) includes breaking changes. An early casualty was Facebook’s Ax (Adaptive Experimentation Platform), with the report that “it seems that Ax is pulling this new version through dependencies which is breaking Ax‘s own code.” The suggested temporary fix is to “limit NumPy version to ‘<2.0.0’, even if they do not depend directly on NumPy.”

Another developer found that the release “broke my CI job since it was indirectly installed,” a reminder that the consequences of introducing incompatibility in a widely used library can be far-reaching. 

There is a migration guide including a plugin rule for the Ruff linter that will check and adapt much code automatically.

Developers who overcome compatibility issues will find many improvements in NumPy 2.0, including accelerated sort functions which now use the Google Highway C++ library, which on certain hardware can produce a large speed up. On macOS the Apple Accelerate library is now used for a performance boost. 

There is also a new variable-length string dtype (data type object), and a new public C API for creating custom dtypes.