NumPy 2.1.0 Release Notes#
Highlights#
We’ll choose highlights for this release near the end of the release cycle.
New functions#
New function numpy.unstack
#
A new function np.unstack(array, axis=...)
was added, which splits
an array into a tuple of arrays along an axis. It serves as the inverse
of numpy.stack
.
(gh-26579)
Deprecations#
The fix_imports keyword argument in
numpy.save
is deprecated. Since NumPy 1.17,numpy.save
uses a pickle protocol that no longer supports Python 2, and ignored fix_imports keyword. This keyword is kept only for backward compatibility. It is now deprecated.
(gh-26452)
Expired deprecations#
Scalars and 0D arrays are disallowed for
numpy.nonzero
andnumpy.ndarray.nonzero
.(gh-26268)
set_string_function
internal function was removed andPyArray_SetStringFunction
was stubbed out.(gh-26611)
C API changes#
Many shims removed from npy_3kcompat.h#
Many of the old shims and helper functions were removed from
npy_3kcompat.h
. If you find yourself in need of these, vendor the previous
version of the file into your codebase.
(gh-26842)
New PyUFuncObject
field process_core_dims_func
#
The field process_core_dims_func
was added to the structure
PyUFuncObject
. For generalized ufuncs, this field can be set to a
function of type PyUFunc_ProcessCoreDimsFunc
that will be called when the
ufunc is called. It allows the ufunc author to check that core dimensions
satisfy additional constraints, and to set output core dimension sizes if they
have not been provided.
(gh-26908)
New Features#
numpy.reshape
andnumpy.ndarray.reshape
now supportshape
andcopy
arguments.(gh-26292)
NumPy now supports DLPack v1, support for older versions will be deprecated in the future.
(gh-26501)
numpy.asanyarray
now supportscopy
anddevice
arguments, matchingnumpy.asarray
.(gh-26580)
numpy.printoptions
,numpy.get_printoptions
, andnumpy.set_printoptions
now support a new option,override_repr
, for defining customrepr(array)
behavior.(gh-26611)
numpy.cumulative_sum
andnumpy.cumulative_prod
were added as Array API compatible alternatives fornumpy.cumsum
andnumpy.cumprod
. The new functions can include a fixed initial (zeros forsum
and ones forprod
) in the result.numpy.clip
now supportsmax
andmin
keyword arguments which are meant to replacea_min
anda_max
. Also, fornp.clip(a)
ornp.clip(a, None, None)
a copy of the input array will be returned instead of raising an error.numpy.astype
now supportsdevice
argument.(gh-26724)
f2py
can generate freethreading-compatible C extensions#
Pass --freethreading-compatible
to the f2py CLI tool to produce a C
extension marked as compatible with the free threading CPython
interpreter. Doing so prevents the interpreter from re-enabling the GIL at
runtime when it imports the C extension. Note that f2py
does not analyze
fortran code for thread safety, so you must verify that the wrapped fortran
code is thread safe before marking the extension as compatible.
(gh-26981)
Improvements#
histogram
auto-binning now returns bin sizes >=1 for integer input data#
For integer input data, bin sizes smaller than 1 result in spurious empty
bins. This is now avoided when the number of bins is computed using one of the
algorithms provided by histogram_bin_edges
.
(gh-12150)
np.quantile with method closest_observation
chooses nearest even order statistic#
This changes the definition of nearest for border cases from the nearest odd order statistic to nearest even order statistic. The numpy implementation now matches other reference implementations.
(gh-26656)
lapack_lite is now thread safe#
NumPy provides a minimal low-performance version of LAPACK named lapack_lite
that can be used if no BLAS/LAPACK system is detected at build time.
Until now, lapack_lite
was not thread safe. Single-threaded use cases did
not hit any issues, but running linear algebra operations in multiple threads
could lead to errors, incorrect results, or seg faults due to data races.
We have added a global lock, serializing access to lapack_lite
in multiple
threads.
(gh-26750)
The numpy.printoptions
context manager is now thread and async-safe#
In prior versions of NumPy, the printoptions were defined using a combination
of Python and C global variables. We have refactored so the state is stored in
a python ContextVar
, making the context manager thread and async-safe.
(gh-26846)
Performance improvements and changes#
ma.cov
and ma.corrcoef
are now significantly faster#
The private function has been refactored along with ma.cov
and
ma.corrcoef
. They are now significantly faster, particularly on large,
masked arrays.
(gh-26285)
numpy.save
now uses pickle protocol version 4 for saving arrays with object dtype, which allows for pickle objects larger than 4GB and improves saving speed by about 5% for large arrays.
(gh-26388)
Changes#
As
numpy.vecdot
is now a ufunc it has a less precise signature. This is due to the limitations of ufunc’s typing stub.(gh-26313)
numpy.floor
,numpy.ceil
, andnumpy.trunc
now won’t perform casting to a floating dtype for integer and boolean dtype input arrays.(gh-26766)
ma.corrcoef
may return a slightly different result#
A pairwise observation approach is currently used in ma.corrcoef
to
calculate the standard deviations for each pair of variables. This has been
changed as it is being used to normalise the covariance, estimated using
ma.cov
, which does not consider the observations for each variable in a
pairwise manner, rendering it unnecessary. The normalisation has been
replaced by the more appropriate standard deviation for each variable,
which significantly reduces the wall time, but will return slightly different
estimates of the correlation coefficients in cases where the observations
between a pair of variables are not aligned. However, it will return the same
estimates in all other cases, including returning the same correlation matrix
as corrcoef
when using a masked array with no masked values.
(gh-26285)