The NumPy 2.3.0 release continues the work to improve free threaded
Python support and annotations together with the usual set of bug fixes.
It is unusual in the number of expired deprecations, code
modernizations, and style cleanups. The latter may not be visible to
users, but is important for code maintenance over the long term. Note
that we have also upgraded from manylinux2014 to manylinux_2_28.
Users running on a Mac having an M4 cpu might see various warnings about
invalid values and such. The warnings are a known problem with
Accelerate. They are annoying, but otherwise harmless. Apple promises to
fix them.
This release supports Python versions 3.11-3.13, Python 3.14 will be
supported when it is released.
Highlights
Interactive examples in the NumPy documentation.
Building NumPy with OpenMP Parallelization.
Preliminary support for Windows on ARM.
Improved support for free threaded Python.
Improved annotations.
New functions
New function numpy.strings.slice
The new function numpy.strings.slice was added, which implements fast
native slicing of string arrays. It supports the full slicing API
including negative slice offsets and steps.
The numpy.typing.mypy_plugin has been deprecated in favor of
platform-agnostic static type inference. Please remove
numpy.typing.mypy_plugin from the plugins section of your mypy
configuration. If this change results in new errors being reported,
kindly open an issue.
The numpy.typing.NBitBase type has been deprecated and will be
removed in a future version.
This type was previously intended to be used as a generic upper
bound for type-parameters, for example:
import numpy as np
import numpy.typing as npt
def f[NT: npt.NBitBase](x: np.complexfloating[NT]) -> np.floating[NT]: ...
But in NumPy 2.2.0, float64 and complex128 were changed to
concrete subtypes, causing static type-checkers to reject
x: np.float64 = f(np.complex128(42j)).
So instead, the better approach is to use typing.overload:
Remove alias generate_divbyzero_error to
npy_set_floatstatus_divbyzero and generate_overflow_error to
npy_set_floatstatus_overflow (deprecated since 1.10)
datetime64 and timedelta64 construction with a tuple no longer
accepts an event value, either use a two-tuple of (unit, num) or a
4-tuple of (unit, num, den, 1) (deprecated since 1.14)
When constructing a dtype from a class with a dtype attribute,
that attribute must be a dtype-instance rather than a thing that can
be parsed as a dtype instance (deprecated in 1.19). At some point
the whole construct of using a dtype attribute will be deprecated
(see #25306)
NpyIter_GetTransferFlags is now available to check if the iterator
needs the Python API or if casts may cause floating point errors
(FPE). FPEs can for example be set when casting float64(1e300) to
float32 (overflow to infinity) or a NaN to an integer (invalid
value).
New NpyIter_GetTransferFlags and NpyIter_IterationNeedsAPI change
NumPy now has the new NpyIter_GetTransferFlags function as a more
precise way checking of iterator/buffering needs. I.e. whether the
Python API/GIL is required or floating point errors may occur. This
function is also faster if you already know your needs without
buffering.
The NpyIter_IterationNeedsAPI function now performs all the checks
that were previously performed at setup time. While it was never
necessary to call it multiple times, doing so will now have a larger
cost.
The type parameter of np.dtype now defaults to typing.Any. This
way, static type-checkers will infer dtype: np.dtype as
dtype: np.dtype[Any], without reporting an error.
NumPy now registers its pkg-config paths with the pkgconf PyPI package
The pkgconf PyPI
package provides an interface for projects like NumPy to register their
own paths to be added to the pkg-config search path. This means that
when using pkgconf
from PyPI, NumPy will be discoverable without needing for any custom
environment configuration.
[!NOTE]
This only applies when using the pkgconf package from PyPI,
or put another way, this only applies when installing pkgconf via a
Python package manager.
If you are using pkg-config or pkgconf provided by your system,
or any other source that does not use the pkgconf-pypi
project, the NumPy pkg-config directory will not be automatically added
to the search path. In these situations, you might want to use numpy-config.
NumPy has the sometimes difficult behavior that it currently usually
returns scalars rather than 0-D arrays (even if the inputs were 0-D
arrays). This is especially problematic for non-numerical dtypes (e.g.
object).
For ufuncs (i.e. most simple math functions) it is now possible to use
out=... (literally `...`, e.g. out=Ellipsis) which is identical
in behavior to out not being passed, but will ensure a non-scalar
return. This spelling is borrowed from arr1d[0, ...] where the ...
also ensures a non-scalar return.
Other functions with an out= kwarg should gain support eventually.
Downstream libraries that interoperate via __array_ufunc__ or
__array_function__ may need to adapt to support this.
NumPy now supports OpenMP parallel processing capabilities when built
with the -Denable_openmp=true Meson build flag. This feature is
disabled by default. When enabled, np.sort and np.argsort functions
can utilize OpenMP for parallel thread execution, improving performance
for these operations.
np.dtypes.StringDType is now a generic
type which
accepts a type argument for na_object that defaults to
typing.Never. For example, StringDType(na_object=None) returns a
StringDType[None], and StringDType() returns a
StringDType[typing.Never].
np.unique now tries to use a hash table to find unique values instead
of sorting values before finding unique values. This is limited to
certain dtypes for now, and the function is now faster for those dtypes.
The function now also exposes a sorted parameter to allow returning
unique values as they were found, instead of sorting them afterwards.
Performance improvements to np.sort and np.argsort
np.sort and np.argsort functions now can leverage OpenMP for
parallel thread execution, resulting in up to 3.5x speedups on x86
architectures with AVX2 or AVX-512 instructions. This opt-in feature
requires NumPy to be built with the -Denable_openmp Meson flag. Users
can control the number of threads used by setting the OMP_NUM_THREADS
environment variable.
Earlier, floating point casts to and from np.float16 types were
emulated in software on all platforms.
Now, on ARM devices that support Neon float16 intrinsics (such as recent
Apple Silicon), the native float16 path is used to achieve the best
performance.
The vector norm ord=inf and the matrix norms
ord={1, 2, inf, 'nuc'} now always returns zero for empty arrays.
Empty arrays have at least one axis of size zero. This affects
np.linalg.norm, np.linalg.vector_norm, and
np.linalg.matrix_norm. Previously, NumPy would raises errors or
return zero depending on the shape of the array.
Printing of np.float16 and np.float32 scalars and arrays have
been improved by adjusting the transition to scientific notation
based on the floating point precision. A new legacy
np.printoptions mode '2.2' has been added for backwards
compatibility.
Multiplication between a string and integer now raises OverflowError
instead of MemoryError if the result of the multiplication would
create a string that is too large to be represented. This follows
Python's behavior.
The relatively new function (added in NumPy 2.0) unique_values may now
return unsorted results. Just as unique_counts and unique_all these
never guaranteed a sorted result, however, the result was sorted until
now. In cases where these do return a sorted result, this may change in
future releases to improve performance.
Changes to the main iterator and potential numerical changes
The main iterator, used in math functions and via np.nditer from
Python and NpyIter in C, now behaves differently for some buffered
iterations. This means that:
The buffer size used will often be smaller than the maximum buffer
sized allowed by the buffersize parameter.
The "growinner" flag is now honored with buffered reductions when
no operand requires buffering.
For np.sum() such changes in buffersize may slightly change numerical
results of floating point operations. Users who use "growinner" for
custom reductions could notice changes in precision (for example, in
NumPy we removed it from einsum to avoid most precision changes and
improve precision for some 64bit floating point inputs).
The minimum supported version was updated from 8.4.0 to 9.3.0, primarily
in order to reduce the chance of platform-specific bugs in old GCC
versions from causing issues.
Changes to automatic bin selection in numpy.histogram
The automatic bin selection algorithm in numpy.histogram has been
modified to avoid out-of-memory errors for samples with low variation.
For full control over the selected bins the user can use set the bin
or range parameters of numpy.histogram.
Wheels for linux systems will use the manylinux_2_28 tag (instead of
the manylinux2014 tag), which means dropping support for
redhat7/centos7, amazonlinux2, debian9, ubuntu18.04, and other
pre-glibc2.28 operating system versions, as per the PEP 600 support
table.
Remove use of -Wl,-ld_classic on macOS. This hack is no longer needed by
Spack, and results in libraries that cannot link to other libraries
built with ld (new).