NumPy 2.2.0 Release Notes#

The NumPy 2.2.0 release is quick release that brings us back into sync with theusual twice yearly release cycle. There have been an number of small cleanups,as well as work bringing the new StringDType to completion and improving supportfor free threaded Python. Highlights are:

  • New functionsmatvec andvecmat, see below.

  • Many improved annotations.

  • Improved support for the new StringDType.

  • Improved support for free threaded Python

  • Fixes for f2py

This release supports Python versions 3.10-3.13.

Deprecations#

  • _add_newdoc_ufunc is now deprecated.ufunc.__doc__=newdoc shouldbe used instead.

    (gh-27735)

Expired deprecations#

  • bool(np.array([])) and other empty arrays will now raise an error.Usearr.size>0 instead to check whether an array has no elements.

    (gh-27160)

Compatibility notes#

  • numpy.cov now properly transposes single-row (2d array) design matriceswhenrowvar=False. Previously, single-row design matrices would return ascalar in this scenario, which is not correct, so this is a behavior changeand an array of the appropriate shape will now be returned.

    (gh-27661)

New Features#

  • New functions for matrix-vector and vector-matrix products

    Two new generalized ufuncs were defined:

    • numpy.matvec - matrix-vector product, treating the arguments asstacks of matrices and column vectors, respectively.

    • numpy.vecmat - vector-matrix product, treating the arguments asstacks of column vectors and matrices, respectively. For complexvectors, the conjugate is taken.

    These add to the existingnumpy.matmul as well as tonumpy.vecdot,which was added in numpy 2.0.

    Note thatnumpy.matmul never takes a complex conjugate, also notwhen its left input is a vector, while bothnumpy.vecdot andnumpy.vecmat do take the conjugate for complex vectors on theleft-hand side (which are taken to be the ones that are transposed,following the physics convention).

    (gh-25675)

  • np.complexfloating[T,T] can now also be written asnp.complexfloating[T]

    (gh-27420)

  • UFuncs now support__dict__ attribute and allow overriding__doc__(either directly or viaufunc.__dict__["__doc__"]).__dict__ can beused to also override other properties, such as__module__ or__qualname__.

    (gh-27735)

  • The “nbit” type parameter ofnp.number and its subtypes now defaultstotyping.Any. This way, type-checkers will infer annotations such asx:np.floating asx:np.floating[Any], even in strict mode.

    (gh-27736)

Improvements#

  • Thedatetime64 andtimedelta64 hashes now correctly match the Pythonsbuiltindatetime andtimedelta ones. The hashes now evaluated equaleven for equal values with different time units.

    (gh-14622)

  • Fixed a number of issues around promotion for string ufuncs with StringDTypearguments. Mixing StringDType and the fixed-width DTypes using the stringufuncs should now generate much more uniform results.

    (gh-27636)

  • Improved support for emptymemmap. Previously an emptymemmap would failunless a non-zerooffset was set. Now a zero-sizememmap is supportedeven ifoffset=0. To achieve this, if amemmap is mapped to an emptyfile that file is padded with a single byte.

    (gh-27723)

f2py handles multiple modules and exposes variables again#

A regression has been fixed which allows F2PY users to expose variables toPython in modules with only assignments, and also fixes situations wheremultiple modules are present within a single source file.

(gh-27695)

Performance improvements and changes#

  • Improved multithreaded scaling on the free-threaded build when many threadssimultaneously call the same ufunc operations.

    (gh-27896)

  • NumPy now uses fast-on-failure attribute lookups for protocols. This cangreatly reduce overheads of function calls or array creation especially withcustom Python objects. The largest improvements will be seen on Python 3.12or newer.

    (gh-27119)

  • OpenBLAS on x86_64 and i686 is built with fewer kernels. Based onbenchmarking, there are 5 clusters of performance around these kernels:PRESCOTTNEHALEMSANDYBRIDGEHASWELLSKYLAKEX.

  • OpenBLAS on windows is linked without quadmath, simplifying licensing

  • Due to a regression in OpenBLAS on windows, the performance improvementswhen using multiple threads for OpenBLAS 0.3.26 were reverted.

    (gh-27147)

  • NumPy now indicates hugepages also for largenp.zeros allocationson linux. Thus should generally improve performance.

    (gh-27808)

Changes#

  • numpy.fix now won’t perform casting to a floating data-type for integerand boolean data-type input arrays.

    (gh-26766)

  • The type annotations ofnumpy.float64 andnumpy.complex128 nowreflect that they are also subtypes of the built-infloat andcomplextypes, respectively. This update prevents static type-checkers from reportingerrors in cases such as:

    x:float=numpy.float64(6.28)# validz:complex=numpy.complex128(-1j)# valid

    (gh-27334)

  • Therepr of arrays large enough to be summarized (i.e., where elementsare replaced with...) now includes theshape of the array, similarto what already was the case for arrays with zero size and non-obviousshape. With this change, the shape is always given when it cannot beinferred from the values. Note that while written asshape=..., thisargument cannot actually be passed in to thenp.array constructor. Ifyou encounter problems, e.g., due to failing doctests, you can use the printoptionlegacy=2.1 to get the old behaviour.

    (gh-27482)

  • Calling__array_wrap__ directly on NumPy arrays or scalars now does theright thing whenreturn_scalar is passed (Added in NumPy 2). It isfurther safe now to call the scalar__array_wrap__ on a non-scalarresult.

    (gh-27807)

Bump the musllinux CI image and wheels to 1_2 from 1_1. This is because 1_1 isend of life.

(gh-27088)

NEP 50 promotion state option removed#

The NEP 50 promotion state settings are now removed. They were always meant astemporary means for testing. A warning will be given if the environmentvariable is set to anything butNPY_PROMOTION_STATE=weak while_set_promotion_state and_get_promotion_state are removed. In casecode used_no_nep50_warning, acontextlib.nullcontext could be used toreplace it when not available.

(gh-27156)