NumPy 2.0.0 Release Notes#
NumPy 2.0.0 is the first major release since 2006. It is the result of 11months of development since the last feature release and is the work of 212contributors spread over 1078 pull requests. It contains a large number ofexciting new features as well as changes to both the Python and C APIs.
This major release includes breaking changes that could not happen in a regularminor (feature) release - including an ABI break, changes to type promotionrules, and API changes which may not have been emitting deprecation warningsin 1.26.x. Key documents related to how to adapt to changes in NumPy 2.0, inaddition to these release notes, include:
Highlights#
Highlights of this release include:
New features:
A new variable-length string dtype,
StringDTypeand a newnumpy.stringsnamespace with performant ufuncs for string operations,Support for
float32andlongdoublein allnumpy.fftfunctions,Support for the array API standard in the main
numpynamespace.
Performance improvements:
Sorting functions (
sort,argsort,partition,argpartition)have been accelerated through the use of the Intel x86-simd-sort and GoogleHighway libraries, and may see large (hardware-specific) speedups,macOS Accelerate support and binary wheels for macOS >=14, with significantperformance improvements for linear algebra operations on macOS, and wheelsthat are about 3 times smaller,
numpy.charfixed-length string operations have been accelerated byimplementing ufuncs that also supportStringDTypeinaddition to the fixed-length string dtypes,A new tracing and introspection API,
opt_func_info,to determine which hardware-specific kernels are available and will bedispatched to.numpy.savenow uses pickle protocol version 4 for saving arrays withobject dtype, which allows for pickle objects larger than 4GB and improvessaving speed by about 5% for large arrays.
Python API improvements:
A clear split between public and private API, with a newmodule structure, and each public function nowavailable in a single place,
Many removals of non-recommended functions and aliases. This should makeit easier to learn and use NumPy. The number of objects in the mainnamespace decreased by ~10% and in
numpy.libby ~80%,Canonical dtype names and a new
isdtypeintrospection function,
C API improvements:
Many outdated functions and macros removed, and private internals hidden toease future extensibility,
New, easier to use, initialization functions:
PyArray_ImportNumPyAPIandPyUFunc_ImportUFuncAPI.
Improved behavior:
Improvements to type promotion behavior was changed by adoptingNEP50. This fixes many user surprises about promotions whichpreviously often depended on data values of input arrays rather than onlytheir dtypes. Please see the NEP and theNumPy 2.0 migration guidefor details as this change can lead to changes in output dtypes and lowerprecision results for mixed-dtype operations.
The default integer type on Windows is now
int64rather thanint32,matching the behavior on other platforms,The maximum number of array dimensions is changed from 32 to 64
Documentation:
The reference guide navigation was significantly improved, and there is nowdocumentation on NumPy’smodule structure,
Thebuilding from source documentation wascompletely rewritten,
Furthermore there are many changes to NumPy internals, including continuing tomigrate code from C to C++, that will make it easier to improve and maintainNumPy in the future.
The “no free lunch” theorem dictates that there is a price to pay for all theseAPI and behavior improvements and better future extensibility. This price is:
Backwards compatibility. There are a significant number of breaking changesto both the Python and C APIs. In the majority of cases, there are clearerror messages that will inform the user how to adapt their code. However,there are also changes in behavior for which it was not possible to givesuch an error message - these cases are all covered in the Deprecation andCompatibility sections below, and in theNumPy 2.0 migration guide.
Note that there is a
ruffmode to auto-fix many things in Python code.Breaking changes to the NumPy ABI. As a result, binaries of packages thatuse the NumPy C API and were built against a NumPy 1.xx release will notwork with NumPy 2.0. On import, such packages will see an
ImportErrorwith a message about binary incompatibility.It is possible to build binaries against NumPy 2.0 that will work at runtimewith both NumPy 2.0 and 1.x. SeeNumPy 2.0-specific advice for more details.
All downstream packages that depend on the NumPy ABI are advised to do anew release built against NumPy 2.0 and verify that that release works withboth 2.0 and 1.26 - ideally in the period between 2.0.0rc1 (which will beABI-stable) and the final 2.0.0 release to avoid problems for their users.
The Python versions supported by this release are 3.9-3.12.
NumPy 2.0 Python API removals#
np.geterrobj,np.seterrobjand the related ufunc keyword argumentextobj=have been removed. The preferred replacement for all of theseis using the context managerwithnp.errstate():.(gh-23922)
np.casthas been removed. The literal replacement fornp.cast[dtype](arg)isnp.asarray(arg,dtype=dtype).np.sourcehas been removed. The preferred replacement isinspect.getsource.np.lookforhas been removed.(gh-24144)
numpy.whohas been removed. As an alternative for the removed functionality, onecan use a variable explorer that is available in IDEs such as Spyder or Jupyter Notebook.(gh-24321)
Warnings and exceptions present in
numpy.exceptions(e.g,ComplexWarning,VisibleDeprecationWarning) are no longer exposed in themain namespace.Multiple niche enums, expired members and functions have been removed fromthe main namespace, such as:
ERR_*,SHIFT_*,np.fastCopyAndTranspose,np.kernel_version,np.numarray,np.oldnumericandnp.set_numeric_ops.(gh-24316)
Replaced
from...import*in thenumpy/__init__.pywith explicit imports.As a result, these main namespace members got removed:np.FLOATING_POINT_SUPPORT,np.FPE_*,np.NINF,np.PINF,np.NZERO,np.PZERO,np.CLIP,np.WRAP,np.WRAP,np.RAISE,np.BUFSIZE,np.UFUNC_BUFSIZE_DEFAULT,np.UFUNC_PYVALS_NAME,np.ALLOW_THREADS,np.MAXDIMS,np.MAY_SHARE_EXACT,np.MAY_SHARE_BOUNDS,add_newdoc,np.add_docstringandnp.add_newdoc_ufunc.(gh-24357)
Alias
np.float_has been removed. Usenp.float64instead.Alias
np.complex_has been removed. Usenp.complex128instead.Alias
np.longfloathas been removed. Usenp.longdoubleinstead.Alias
np.singlecomplexhas been removed. Usenp.complex64instead.Alias
np.cfloathas been removed. Usenp.complex128instead.Alias
np.longcomplexhas been removed. Usenp.clongdoubleinstead.Alias
np.clongfloathas been removed. Usenp.clongdoubleinstead.Alias
np.string_has been removed. Usenp.bytes_instead.Alias
np.unicode_has been removed. Usenp.str_instead.Alias
np.Infhas been removed. Usenp.infinstead.Alias
np.Infinityhas been removed. Usenp.infinstead.Alias
np.NaNhas been removed. Usenp.naninstead.Alias
np.inftyhas been removed. Usenp.infinstead.Alias
np.mathas been removed. Usenp.asmatrixinstead.np.issubclass_has been removed. Use theissubclassbuiltin instead.np.asfarrayhas been removed. Usenp.asarraywith a proper dtype instead.np.set_string_functionhas been removed. Usenp.set_printoptionsinstead with a formatter for custom printing of NumPy objects.np.tracemalloc_domainis now only available fromnp.lib.np.recfromcsvandnp.recfromtxtwere removed from the main namespace.Usenp.genfromtxtwith comma delimiter instead.np.issctype,np.maximum_sctype,np.obj2sctype,np.sctype2char,np.sctypes,np.issubsctypewere all removed from themain namespace without replacement, as they where niche members.Deprecated
np.deprecateandnp.deprecate_with_dochas been removedfrom the main namespace. UseDeprecationWarninginstead.Deprecated
np.safe_evalhas been removed from the main namespace.Useast.literal_evalinstead.(gh-24376)
np.find_common_typehas been removed. Usenumpy.promote_typesornumpy.result_typeinstead. To achieve semantics for thescalar_typesargument, usenumpy.result_typeand pass0,0.0, or0jas aPython scalar instead.np.round_has been removed. Usenp.roundinstead.np.nbyteshas been removed. Usenp.dtype(<dtype>).itemsizeinstead.(gh-24477)
np.compare_chararrayshas been removed from the main namespace.Usenp.char.compare_chararraysinstead.The
charrarrayin the main namespace has been deprecated. It can be importedwithout a deprecation warning fromnp.char.chararrayfor now,but we are planning to fully deprecate and removechararrayin the future.np.format_parserhas been removed from the main namespace.Usenp.rec.format_parserinstead.(gh-24587)
Support for seven data type string aliases has been removed from
np.dtype:int0,uint0,void0,object0,str0,bytes0andbool8.(gh-24807)
The experimental
numpy.array_apisubmodule has been removed. Use the mainnumpynamespace for regular usage instead, or the separatearray-api-strictpackage for the compliance testing use case for whichnumpy.array_apiwas mostly used.(gh-25911)
__array_prepare__ is removed#
UFuncs called__array_prepare__ before running computationsfor normal ufunc calls (not generalized ufuncs, reductions, etc.).The function was also called instead of__array_wrap__ on theresults of some linear algebra functions.
It is now removed. If you use it, migrate to__array_ufunc__ or rely on__array_wrap__ which is called with a context in all cases, although onlyafter the result array is filled. In those code paths,__array_wrap__ willnow be passed a base class, rather than a subclass array.
(gh-25105)
Deprecations#
np.compathas been deprecated, as Python 2 is no longer supported.numpy.int8and similar classes will no longer support conversion ofout of bounds python integers to integer arrays. For example,conversion of 255 to int8 will not return -1.numpy.iinfo(dtype)can be used to check the machine limits for data types.For example,np.iinfo(np.uint16)returns min = 0 and max = 65535.np.array(value).astype(dtype)will give the desired result.np.safe_evalhas been deprecated.ast.literal_evalshould be used instead.(gh-23830)
np.recfromcsv,np.recfromtxt,np.disp,np.get_array_wrap,np.maximum_sctype,np.deprecateandnp.deprecate_with_dochave been deprecated.(gh-24154)
np.trapzhas been deprecated. Usenp.trapezoidor ascipy.integratefunction instead.np.in1dhas been deprecated. Usenp.isininstead.Alias
np.row_stackhas been deprecated. Usenp.vstackdirectly.(gh-24445)
__array_wrap__is now passedarr,context,return_scalarandsupport for implementations not accepting all three are deprecated. Its signatureshould be__array_wrap__(self,arr,context=None,return_scalar=False)(gh-25409)
Arrays of 2-dimensional vectors for
np.crosshave been deprecated. Usearrays of 3-dimensional vectors instead.(gh-24818)
np.dtype("a")alias fornp.dtype(np.bytes_)was deprecated. Usenp.dtype("S")alias instead.(gh-24854)
Use of keyword arguments
xandywith functionsassert_array_equalandassert_array_almost_equalhas been deprecated.Pass the first two arguments as positional arguments instead.(gh-24978)
numpy.fft deprecations for n-D transforms with None values in arguments#
Usingfftn,ifftn,rfftn,irfftn,fft2,ifft2,rfft2 orirfft2 with thes parameter set to a value that is notNone and theaxes parameter set toNone has been deprecated, inline with the array API standard. To retain current behaviour, pass a sequence[0, …, k-1] toaxes for an array of dimension k.
Furthermore, passing an array tos which containsNone values isdeprecated as the parameter is documented to accept a sequence of integersin both the NumPy docs and the array API specification. To use the defaultbehaviour of the corresponding 1-D transform, pass the value matchingthe default for itsn parameter. To use the default behaviour for everyaxis, thes argument can be omitted.
(gh-25495)
np.linalg.lstsq now defaults to a newrcond value#
lstsq now uses the new rcond value of the machine precisiontimesmax(M,N). Previously, the machine precision was used but aFutureWarning was given to notify that this change will happen eventually.That old behavior can still be achieved by passingrcond=-1.
(gh-25721)
Expired deprecations#
The
np.core.umath_testssubmodule has been removed from the public API.(Deprecated in NumPy 1.15)(gh-23809)
The
PyDataMem_SetEventHookdeprecation has expired and it isremoved. Usetracemallocand thenp.lib.tracemalloc_domaindomain. (Deprecated in NumPy 1.23)(gh-23921)
The deprecation of
set_numeric_opsand the C functionsPyArray_SetNumericOpsandPyArray_GetNumericOpshasbeen expired and the functions removed. (Deprecated in NumPy 1.16)(gh-23998)
The
fasttake,fastclip, andfastputmaskArrFuncsdeprecation is now finalized.The deprecated function
fastCopyAndTransposeand its C counterpartare now removed.The deprecation of
PyArray_ScalarFromObjectis now finalized.(gh-24312)
np.msorthas been removed. For a replacement,np.sort(a,axis=0)should be used instead.(gh-24494)
np.dtype(("f8",1)will now return a shape 1 subarray dtyperather than a non-subarray one.(gh-25761)
Assigning to the
.dataattribute of an ndarray is disallowed and willraise.np.binary_repr(a,width)will raise if width is too small.Using
NPY_CHARinPyArray_DescrFromType()will raise, useNPY_STRINGNPY_UNICODE, orNPY_VSTRINGinstead.(gh-25794)
Compatibility notes#
loadtxt andgenfromtxt default encoding changed#
loadtxt andgenfromtxt now both default toencoding=Nonewhich may mainly modify howconverters work.These will now be passedstr rather thanbytes. Pass theencoding explicitly to always get the new or old behavior.Forgenfromtxt the change also means that returned values will now beunicode strings rather than bytes.
(gh-25158)
f2py compatibility notes#
f2pywill no longer accept ambiguous-mand.pyfCLIcombinations. When more than one.pyffile is passed, an error israised. When both-mand a.pyfis passed, a warning is emitted andthe-mprovided name is ignored.(gh-25181)
The
f2py.compile()helper has been removed because it leaked memory, hasbeen marked as experimental for several years now, and was implemented as athinsubprocess.runwrapper. It was also one of the test bottlenecks. Seegh-25122 for the fullrationale. It also used severalnp.distutilsfeatures which are toofragile to be ported to work withmeson.Users are urged to replace calls to
f2py.compilewith calls tosubprocess.run("python","-m","numpy.f2py",...instead, and to useenvironment variables to interact withmeson.Native files are also an option.(gh-25193)
Minor changes in behavior of sorting functions#
Due to algorithmic changes and use of SIMD code, sorting functions with methodsthat aren’t stable may return slightly different results in 2.0.0 compared to1.26.x. This includes the default method ofargsort andargpartition.
Removed ambiguity when broadcasting innp.solve#
The broadcasting rules fornp.solve(a,b) were ambiguous whenb had 1fewer dimensions thana. This has been resolved in a backward-incompatibleway and is now compliant with the Array API. The old behaviour can bereconstructed by usingnp.solve(a,b[...,None])[...,0].
(gh-25914)
Modified representation forPolynomial#
The representation method forPolynomial wasupdated to include the domain in the representation. The plain text and latexrepresentations are now consistent. For example the output ofstr(np.polynomial.Polynomial([1,1],domain=[.1,.2])) used to be1.0+1.0x, but now is1.0+1.0(-3.0000000000000004+20.0x).
(gh-21760)
C API changes#
The
PyArray_CGT,PyArray_CLT,PyArray_CGE,PyArray_CLE,PyArray_CEQ,PyArray_CNEmacros have been removed.PyArray_MINandPyArray_MAXhave been moved fromndarraytypes.htonpy_math.h.(gh-24258)
A C API for working with
numpy.dtypes.StringDTypearrays has been exposed.This includes functions for acquiring and releasing mutexes which lock accessto the string data, as well as packing and unpacking UTF-8 bytestreams fromarray entries.NPY_NTYPEShas been renamed toNPY_NTYPES_LEGACYas it does notinclude new NumPy built-in DTypes. In particular the new string DTypewill likely not work correctly with code that handles legacy DTypes.(gh-25347)
The C-API now only exports the static inline function versionsof the array accessors (previously this depended on using “deprecated API”).While we discourage it, the struct fields can still be used directly.
(gh-25789)
NumPy now defines
PyArray_Packto set an individual memoryaddress. UnlikePyArray_SETITEMthis function is equivalent to settingan individual array item and does not require a NumPy array input.(gh-25954)
The
->fslot has been removed fromPyArray_Descr.If you use this slot, replace accessing it withPyDataType_GetArrFuncs(see its documentation and theNumPy 2.0 migration guide). In some cases using other functions likePyArray_GETITEMmay be an alternatives.PyArray_GETITEMandPyArray_SETITEMnow require the import of theNumPy API table to be used and are no longer defined inndarraytypes.h.(gh-25812)
Due to runtime dependencies, the definition for functionality accessingthe dtype flags was moved from
numpy/ndarraytypes.hand is only availableafter includingnumpy/ndarrayobject.has it requiresimport_array().This includesPyDataType_FLAGCHK,PyDataType_REFCHKandNPY_BEGIN_THREADS_DESCR.The dtype flags on
PyArray_Descrmust now be accessed through thePyDataType_FLAGSinline function to be compatible with both 1.x and 2.x.This function is defined innpy_2_compat.hto allow backporting.Most or all users should usePyDataType_FLAGCHKwhich is available on1.x and does not require backporting.Cython users should use Cython 3. Otherwise access will go through Pythonunless they usePyDataType_FLAGCHKinstead.(gh-25816)
Datetime functionality exposed in the C API and Cython bindings#
The functionsNpyDatetime_ConvertDatetime64ToDatetimeStruct,NpyDatetime_ConvertDatetimeStructToDatetime64,NpyDatetime_ConvertPyDateTimeToDatetimeStruct,NpyDatetime_GetDatetimeISO8601StrLen,NpyDatetime_MakeISO8601Datetime,andNpyDatetime_ParseISO8601Datetime have been added to the C API tofacilitate converting between strings, Python datetimes, and NumPy datetimes inexternal libraries.
(gh-21199)
Const correctness for the generalized ufunc C API#
The NumPy C API’s functions for constructing generalized ufuncs(PyUFunc_FromFuncAndData,PyUFunc_FromFuncAndDataAndSignature,PyUFunc_FromFuncAndDataAndSignatureAndIdentity) taketypes anddataarguments that are not modified by NumPy’s internals. Like thename anddoc arguments, third-party Python extension modules are likely to supplythese arguments from static constants. Thetypes anddata arguments arenow const-correct: they are declared asconstchar*types andvoid*const*data, respectively. C code should not be affected, but C++code may be.
(gh-23847)
LargerNPY_MAXDIMS andNPY_MAXARGS,NPY_RAVEL_AXIS introduced#
NPY_MAXDIMS is now 64, you may want to review its use. This is usuallyused in a stack allocation, where the increase should be safe.However, we do encourage generally to remove any use ofNPY_MAXDIMS andNPY_MAXARGS to eventually allow removing the constraint completely.For the conversion helper and C-API functions mirroring Python ones such astake,NPY_MAXDIMS was used to meanaxis=None. Such usage must bereplaced withNPY_RAVEL_AXIS. See alsoIncreased maximum number of dimensions.
(gh-25149)
NPY_MAXARGS not constant andPyArrayMultiIterObject size change#
SinceNPY_MAXARGS was increased, it is now a runtime constant and notcompile-time constant anymore.We expect almost no users to notice this. But if used for stack allocationsit now must be replaced with a custom constant usingNPY_MAXARGS as anadditional runtime check.
Thesizeof(PyArrayMultiIterObject) no longer includes the full sizeof the object. We expect nobody to notice this change. It was necessaryto avoid issues with Cython.
(gh-25271)
Required changes for custom legacy user dtypes#
In order to improve our DTypes it is unfortunately necessaryto break the ABI, which requires some changes for dtypes registeredwithPyArray_RegisterDataType.Please see the documentation ofPyArray_RegisterDataType for howto adapt your code and achieve compatibility with both 1.x and 2.x.
(gh-25792)
New Public DType API#
The C implementation of the NEP 42 DType API is now public. While the DType APIhas shipped in NumPy for a few versions, it was only usable in sessions with aspecial environment variable set. It is now possible to write custom DTypesoutside of NumPy using the new DType API and the normalimport_array()mechanism for importing the numpy C API.
SeeCustom Data Types for more details about the API. As always with a newfeature, please report any bugs you run into implementing or using a newDType. It is likely that downstream C code that works with dtypes will need tobe updated to work correctly with new DTypes.
(gh-25754)
New C-API import functions#
We have now addedPyArray_ImportNumPyAPI andPyUFunc_ImportUFuncAPIas static inline functions to import the NumPy C-API tables.The new functions have two advantages overimport_array andimport_ufunc:
They check whether the import was already performed and are light-weightif not, allowing to add them judiciously (although this is not preferablein most cases).
The old mechanisms were macros rather than functions which included a
returnstatement.
ThePyArray_ImportNumPyAPI() function is included innpy_2_compat.hfor simpler backporting.
(gh-25866)
Structured dtype information access through functions#
The dtype structures fieldsc_metadata,names,fields, andsubarray must now be accessed through newfunctions following the same names, such asPyDataType_NAMES.Direct access of the fields is not valid as they do not exist forallPyArray_Descr instances.Themetadata field is kept, but the macro version should also be preferred.
(gh-25802)
Descriptorelsize andalignment access#
Unless compiling only with NumPy 2 support, theelsize andalignmentfields must now be accessed viaPyDataType_ELSIZE,PyDataType_SET_ELSIZE, andPyDataType_ALIGNMENT.In cases where the descriptor is attached to an array, we adviseusingPyArray_ITEMSIZE as it exists on all NumPy versions.Please seeThe PyArray_Descr struct has been changed for more information.
(gh-25943)
NumPy 2.0 C API removals#
npy_interrupt.hand the corresponding macros likeNPY_SIGINT_ONhave been removed. We recommend queryingPyErr_CheckSignals()orPyOS_InterruptOccurred()periodically (these do currently requireholding the GIL though).The
noprefix.hheader has been removed. Replace missing symbols withtheir prefixed counterparts (usually an addedNPY_ornpy_).(gh-23919)
PyUFunc_GetPyVals,PyUFunc_handlefperr, andPyUFunc_checkfperrhave been removed.If needed, a new backwards compatible function to raise floating point errorscould be restored. Reason for removal: there are no known users and thefunctions would have madewithnp.errstate()fixes much more difficult).(gh-23922)
The
numpy/old_defines.hwhich was part of the API deprecated since NumPy 1.7has been removed. This removes macros of the formPyArray_CONSTANT.Thereplace_old_macros.sedscript may be useful to convert them to theNPY_CONSTANTversion.(gh-24011)
The
legacy_inner_loop_selectormember of the ufunc struct is removedto simplify improvements to the dispatching system.There are no known users overriding or directly accessing this member.(gh-24271)
NPY_INTPLTRhas been removed to avoid confusion (seeintpredefinition).(gh-24888)
The advanced indexing
MapIterand related API has been removed.The (truly) public part of it was not well tested and had only oneknown user (Theano). Making it private will simplify improvementsto speed upufunc.at, make advanced indexing more maintainable,and was important for increasing the maximum number of dimensions of arraysto 64. Please let us know if this API is important to you so we can find asolution together.(gh-25138)
The
NPY_MAX_ELSIZEmacro has been removed, as it only ever reflectedbuiltin numeric types and served no internal purpose.(gh-25149)
PyArray_REFCNTandNPY_REFCOUNTare removed. UsePy_REFCNTinstead.(gh-25156)
PyArrayFlags_TypeandPyArray_NewFlagsObjectas well asPyArrayFlagsObjectare private now.There is no known use-case; use the Python API if needed.PyArray_MoveInto,PyArray_CastTo,PyArray_CastAnyToare removedusePyArray_CopyIntoand if absolutely neededPyArray_CopyAnyInto(the latter does a flat copy).PyArray_FillObjectArrayis removed, its only true use was forimplementingnp.empty. Create a new empty array or usePyArray_FillWithScalar()(decrefs existing objects).PyArray_CompareUCS4andPyArray_CompareStringare removed.Use the standard C string comparison functions.PyArray_ISPYTHONis removed as it is misleading, has no knownuse-cases, and is easy to replace.PyArray_FieldNamesis removed, as it is unclear what it wouldbe useful for. It also has incorrect semantics in some possibleuse-cases.PyArray_TypestrConvertis removed, since it seems a misnomer and unlikelyto be used by anyone. If you know the size or are limited to few types, justuse it explicitly, otherwise go via Python strings.(gh-25292)
PyDataType_GetDatetimeMetaDatais removed, it did not actuallydo anything since at least NumPy 1.7.(gh-25802)
PyArray_GetCastFuncis removed. Note that custom legacy user dtypescan still provide a castfunc as their implementation, but any access to themis now removed. The reason for this is that NumPy never used theseinternally for many years. If you use simple numeric types, please just useC casts directly. In case you require an alternative, please let us know sowe can create new API such asPyArray_CastBuffer()which could use old ornew cast functions depending on the NumPy version.(gh-25161)
New Features#
np.add was extended to work withunicode andbytes dtypes.#
(gh-24858)
A newbitwise_count function#
This new function counts the number of 1-bits in a number.bitwise_count works on all the numpy integer types andinteger-like objects.
>>>a=np.array([2**i-1foriinrange(16)])>>>np.bitwise_count(a)array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], dtype=uint8)
(gh-19355)
macOS Accelerate support, including the ILP64#
Support for the updated Accelerate BLAS/LAPACK library, including ILP64 (64-bitinteger) support, in macOS 13.3 has been added. This brings arm64 support, andsignificant performance improvements of up to 10x for commonly used linearalgebra operations. When Accelerate is selected at build time, or if noexplicit BLAS library selection is done, the 13.3+ version will automaticallybe used if available.
(gh-24053)
Binary wheels are also available. On macOS >=14.0, users who install NumPy fromPyPI will get wheels built against Accelerate rather than OpenBLAS.
(gh-25255)
Option to use weights for quantile and percentile functions#
Aweights keyword is now available forquantile,percentile,nanquantile andnanpercentile. Onlymethod="inverted_cdf" supports weights.
(gh-24254)
Improved CPU optimization tracking#
A new tracer mechanism is available which enables tracking of the enabledtargets for each optimized function (i.e., that uses hardware-specific SIMDinstructions) in the NumPy library. With this enhancement, it becomes possibleto precisely monitor the enabled CPU dispatch targets for the dispatchedfunctions.
A new function namedopt_func_info has been added to the new namespacenumpy.lib.introspect, offering this tracing capability. This function allowsyou to retrieve information about the enabled targets based on function namesand data type signatures.
(gh-24420)
A new Meson backend forf2py#
f2py in compile mode (i.e.f2py-c) now accepts the--backendmesonoption. This is the default option for Python >=3.12. For older Python versions,f2py will still default to--backenddistutils.
To support this in realistic use-cases, in compile modef2py takes a--dep flag one or many times which maps todependency() calls in themeson backend, and does nothing in thedistutils backend.
There are no changes for users off2py only as a code generator, i.e. without-c.
(gh-24532)
bind(c) support forf2py#
Both functions and subroutines can be annotated withbind(c).f2py willhandle both the correct type mapping, and preserve the unique label for otherC interfaces.
Note:bind(c,name='routine_name_other_than_fortran_routine') is nothonored by thef2py bindings by design, sincebind(c) with thenameis meant to guarantee only the same name in C and Fortran, not in Python andFortran.
(gh-24555)
A newstrict option for several testing functions#
Thestrict keyword is now available forassert_allclose,assert_equal, andassert_array_less.Settingstrict=True will disable the broadcasting behaviour for scalarsand ensure that input arrays have the same data type.
Addnp.core.umath.find andnp.core.umath.rfind UFuncs#
Add twofind andrfind UFuncs that operate on unicode or byte stringsand are used innp.char. They operate similar tostr.find andstr.rfind.
(gh-24868)
diagonal andtrace fornumpy.linalg#
numpy.linalg.diagonal andnumpy.linalg.trace have beenadded, which are array API standard-compatible variants ofnumpy.diagonal andnumpy.trace. They differ in the default axis selection which define 2-Dsub-arrays.
(gh-24887)
Newlong andulong dtypes#
numpy.long andnumpy.ulong have been added as NumPy integers mappingto C’slong andunsignedlong. Prior to NumPy 1.24,numpy.long wasan alias to Python’sint.
(gh-24922)
svdvals fornumpy.linalg#
numpy.linalg.svdvals has been added. It computes singular values for(a stack of) matrices. Executingnp.svdvals(x) is the same as callingnp.svd(x,compute_uv=False,hermitian=False).This function is compatible with the array API standard.
(gh-24940)
A newisdtype function#
numpy.isdtype was added to provide a canonical way to classify NumPy’s dtypesin compliance with the array API standard.
(gh-25054)
A newastype function#
numpy.astype was added to provide an array API standard-compatiblealternative to thenumpy.ndarray.astype method.
(gh-25079)
Array API compatible functions’ aliases#
13 aliases for existing functions were added to improve compatibility with the array API standard:
Trigonometry:
acos,acosh,asin,asinh,atan,atanh,atan2.Bitwise:
bitwise_left_shift,bitwise_invert,bitwise_right_shift.Misc:
concat,permute_dims,pow.In
numpy.linalg:tensordot,matmul.
(gh-25086)
Newunique_* functions#
Theunique_all,unique_counts,unique_inverse,andunique_values functions have been added. They providefunctionality ofunique with different sets of flags. They are array APIstandard-compatible, and because the number of arrays they return does notdepend on the values of input arguments, they are easier to target for JITcompilation.
(gh-25088)
Matrix transpose support for ndarrays#
NumPy now offers support for calculating the matrix transpose of an array (orstack of arrays). The matrix transpose is equivalent to swapping the last twoaxes of an array. Bothnp.ndarray andnp.ma.MaskedArray now expose a.mT attribute, and there is a matching newnumpy.matrix_transposefunction.
(gh-23762)
Array API compatible functions fornumpy.linalg#
Six new functions and two aliases were added to improve compatibility withthe Array API standard fornumpy.linalg:
numpy.linalg.matrix_norm- Computes the matrix norm of a matrix (or a stack of matrices).numpy.linalg.vector_norm- Computes the vector norm of a vector (or batch of vectors).numpy.vecdot- Computes the (vector) dot product of two arrays.numpy.linalg.vecdot- An alias fornumpy.vecdot.numpy.linalg.matrix_transpose- An alias fornumpy.matrix_transpose.(gh-25155)
numpy.linalg.outerhas been added. It computes the outer product of twovectors. It differs fromnumpy.outerby accepting one-dimensional arraysonly. This function is compatible with the array API standard.(gh-25101)
numpy.linalg.crosshas been added. It computes the cross product of two(arrays of) 3-dimensional vectors. It differs fromnumpy.crossby acceptingthree-dimensional vectors only. This function is compatible with the arrayAPI standard.(gh-25145)
Acorrection argument forvar andstd#
Acorrection argument was added tovar andstd, which isan array API standard compatible alternative toddof. As both argumentsserve a similar purpose, only one of them can be provided at the same time.
(gh-25169)
ndarray.device andndarray.to_device#
Anndarray.device attribute andndarray.to_device method wereadded tonumpy.ndarray for array API standard compatibility.
Additionally,device keyword-only arguments were added to:asarray,arange,empty,empty_like,eye,full,full_like,linspace,ones,ones_like,zeros, andzeros_like.
For all these new arguments, onlydevice="cpu" is supported.
(gh-25233)
StringDType has been added to NumPy#
We have added a new variable-width UTF-8 encoded string data type, implementinga “NumPy array of Python strings”, including support for a user-provided missingdata sentinel. It is intended as a drop-in replacement for arrays of Pythonstrings and missing data sentinels using the object dtype. SeeNEP 55 andthedocumentation for more details.
(gh-25347)
New keywords forcholesky andpinv#
Theupper andrtol keywords were added tonumpy.linalg.cholesky andnumpy.linalg.pinv, respectively, to improve array API standard compatibility.
Forpinv, if neitherrcond norrtol is specified,thercond’s default is used. We plan to deprecate and removercond inthe future.
(gh-25388)
New keywords forsort,argsort andlinalg.matrix_rank#
New keyword parameters were added to improve array API standard compatibility:
rtolwas added tomatrix_rank.
(gh-25437)
Newnumpy.strings namespace for string ufuncs#
NumPy now implements some string operations as ufuncs. The oldnp.charnamespace is still available, and where possible the string manipulationfunctions in that namespace have been updated to use the new ufuncs,substantially improving their performance.
Where possible, we suggest updating code to use functions innp.stringsinstead ofnp.char. In the future we may deprecatenp.char in favor ofnp.strings.
(gh-25463)
numpy.fft support for different precisions and in-place calculations#
The various FFT routines innumpy.fft now do their calculations natively infloat, double, or long double precision, depending on the input precision,instead of always calculating in double precision. Hence, the calculation willnow be less precise for single and more precise for long double precision.The data type of the output array will now be adjusted accordingly.
Furthermore, all FFT routines have gained anout argument that can be usedfor in-place calculations.
(gh-25536)
configtool and pkg-config support#
A newnumpy-config CLI script is available that can be queried for theNumPy version and for compile flags needed to use the NumPy C API. This willallow build systems to better support the use of NumPy as a dependency.Also, anumpy.pc pkg-config file is now included with Numpy. In order tofind its location for use withPKG_CONFIG_PATH, usenumpy-config--pkgconfigdir.
(gh-25730)
Array API standard support in the main namespace#
The mainnumpy namespace now supports the array API standard. SeeArray API standard compatibility for details.
(gh-25911)
Improvements#
Strings are now supported byany,all, and the logical ufuncs.#
(gh-25651)
Integer sequences as the shape argument formemmap#
numpy.memmap can now be created with any integer sequence as theshapeargument, such as a list or numpy array of integers. Previously, only thetypes of tuple and int could be used without raising an error.
(gh-23729)
errstate is now faster and context safe#
Thenumpy.errstate context manager/decorator is now faster andsafer. Previously, it was not context safe and had (rare)issues with thread-safety.
(gh-23936)
AArch64 quicksort speed improved by using Highway’s VQSort#
The first introduction of the Google Highway library, using VQSort on AArch64.Execution time is improved by up to 16x in some cases, see the PR for benchmarkresults. Extensions to other platforms will be done in the future.
(gh-24018)
Complex types - underlying C type changes#
The underlying C types for all of NumPy’s complex types have been changed touse C99 complex types.
While this change does not affect the memory layout of complex types, itchanges the API to be used to directly retrieve or write the real orcomplex part of the complex number, since direct field access (as in
c.realorc.imag) is no longer an option. You can now use utilities provided innumpy/npy_math.hto do these operations, like this:npy_cdoublec;npy_csetreal(&c,1.0);npy_csetimag(&c,0.0);printf("%d + %di\n",npy_creal(c),npy_cimag(c));
To ease cross-version compatibility, equivalent macros and a compatibilitylayer have been added which can be used by downstream packages to continueto support both NumPy 1.x and 2.x. SeeSupport for complex numbers for more info.
numpy/npy_common.hnow includescomplex.h, which means thatcomplexis now a reserved keyword.
(gh-24085)
iso_c_binding support and improved common blocks forf2py#
Previously, users would have to define their own customf2cmap file to usetype mappings defined by the Fortran2003iso_c_binding intrinsic module.These type maps are now natively supported byf2py
(gh-24555)
f2py now handlescommon blocks which havekind specifications frommodules. This further expands the usability of intrinsics likeiso_fortran_env andiso_c_binding.
(gh-25186)
Callstr automatically on third argument to functions likeassert_equal#
The third argument to functions likeassert_equal now hasstr called on it automatically. This way it mimics the built-inassertstatement, whereassert_equal(a,b,obj) works likeasserta==b,obj.
(gh-24877)
Support for array-likeatol/rtol inisclose,allclose#
The keywordsatol andrtol inisclose andallclosenow accept both scalars and arrays. An array, if given, must broadcastto the shapes of the first two array arguments.
(gh-24878)
Consistent failure messages in test functions#
Previously, somenumpy.testing assertions printed messages thatreferred to the actual and desired results asx andy.Now, these values are consistently referred to asACTUAL andDESIRED.
(gh-24931)
n-D FFT transforms allows[i]==-1#
Thefftn,ifftn,rfftn,irfftn,fft2,ifft2,rfft2andirfft2 functions now use the whole input array along the axisi ifs[i]==-1, in line with the array API standard.
(gh-25495)
Guard PyArrayScalar_VAL and PyUnicodeScalarObject for the limited API#
PyUnicodeScalarObject holds aPyUnicodeObject, which is not availablewhen usingPy_LIMITED_API. Add guards to hide it and consequently also makethePyArrayScalar_VAL macro hidden.
(gh-25531)
Changes#
np.gradient()now returns a tuple rather than a list making thereturn value immutable.(gh-23861)
Being fully context and thread-safe,
np.errstatecan onlybe entered once now.np.setbufsizeis now tied tonp.errstate(): leaving annp.errstatecontext will also reset thebufsize.(gh-23936)
A new public
np.lib.array_utilssubmodule has been introduced and itcurrently contains three functions:byte_bounds(moved fromnp.lib.utils),normalize_axis_tupleandnormalize_axis_index.(gh-24540)
Introduce
numpy.boolas the new canonical name for NumPy’s boolean dtype,and makenumpy.bool_an alias to it. Note that until NumPy 1.24,np.boolwas an alias to Python’s builtinbool. The new name helpswith array API standard compatibility and is a more intuitive name.(gh-25080)
The
dtype.flagsvalue was previously stored as a signed integer.This means that the aligned dtype struct flag lead to negative flags beingset (-128 rather than 128). This flag is now stored unsigned (positive). Codewhich checks flags manually may need to adapt. This may include codecompiled with Cython 0.29.x.(gh-25816)
Representation of NumPy scalars changed#
As perNEP 51, the scalar representation has beenupdated to include the type information to avoid confusion withPython scalars.
Scalars are now printed asnp.float64(3.0) rather than just3.0.This may disrupt workflows that store representations of numbers(e.g., to files) making it harder to read them. They should be stored asexplicit strings, for example by usingstr() orf"{scalar!s}".For the time being, affected users can usenp.set_printoptions(legacy="1.25")to get the old behavior (with possibly a few exceptions).Documentation of downstream projects may require larger updates,if code snippets are tested. We are working on tooling fordoctest-plusto facilitate updates.
(gh-22449)
Truthiness of NumPy strings changed#
NumPy strings previously were inconsistent about how they definedif the string isTrue orFalse and the definition did notmatch the one used by Python.Strings are now consideredTrue when they are non-empty andFalse when they are empty.This changes the following distinct cases:
Casts from string to boolean were previously roughly equivalentto
string_array.astype(np.int64).astype(bool), meaning that onlyvalid integers could be cast.Now a string of"0"will be consideredTruesince it is not empty.If you need the old behavior, you may use the above step (castingto integer first) orstring_array=="0"(if the input is only ever0or1).To get the new result on old NumPy versions usestring_array!="".np.nonzero(string_array)previously ignored whitespace so thata string only containing whitespace was consideredFalse.Whitespace is now consideredTrue.
This change does not affectnp.loadtxt,np.fromstring, ornp.genfromtxt.The first two still use the integer definition, whilegenfromtxt continues tomatch for"true" (ignoring case).However, ifnp.bool_ is used as a converter the result will change.
The change does affectnp.fromregex as it uses direct assignments.
(gh-23871)
Amean keyword was added to var and std function#
Often when the standard deviation is needed the mean is also needed. The sameholds for the variance and the mean. Until now the mean is then calculated twice,the change introduced here for thevar andstd functionsallows for passing in a precalculated mean as an keyword argument. See thedocstrings for details and an example illustrating the speed-up.
(gh-24126)
Remove datetime64 deprecation warning when constructing with timezone#
Thenumpy.datetime64 method now issues a UserWarning rather than aDeprecationWarning whenever a timezone is included in the datetimestring that is provided.
(gh-24193)
Default integer dtype is now 64-bit on 64-bit Windows#
The default NumPy integer is now 64-bit on all 64-bit systems as the historic32-bit default on Windows was a common source of issues. Most users should notnotice this. The main issues may occur with code interfacing with librarieswritten in a compiled language like C. For more information seeWindows default integer.
(gh-24224)
Renamednumpy.core tonumpy._core#
Accessingnumpy.core now emits a DeprecationWarning. In practicewe have found that most downstream usage ofnumpy.core was to accessfunctionality that is available in the mainnumpy namespace.If for some reason you are using functionality innumpy.core thatis not available in the mainnumpy namespace, this means you are likelyusing private NumPy internals. You can still access these internals vianumpy._core without a deprecation warning but we do not provide anybackward compatibility guarantees for NumPy internals. Please open an issueif you think a mistake was made and something needs to be made public.
(gh-24634)
The “relaxed strides” debug build option, which was previously enabled throughtheNPY_RELAXED_STRIDES_DEBUG environment variable or the-Drelaxed-strides-debug config-settings flag has been removed.
(gh-24717)
Redefinition ofnp.intp/np.uintp (almost never a change)#
Due to the actual use of these types almost always matching the use ofsize_t/Py_ssize_t this is now the definition in C.Previously, it matchedintptr_t anduintptr_t which would oftenhave been subtly incorrect.This has no effect on the vast majority of machines since the sizeof these types only differ on extremely niche platforms.
However, it means that:
Pointers may not necessarily fit into an
intptyped array anymore.ThepandPcharacter codes can still be used, however.Creating
intptr_toruintptr_ttyped arrays in C remains possiblein a cross-platform way viaPyArray_DescrFromType('p').The new character codes
nNwere introduced.It is now correct to use the Python C-API functions when parsingto
npy_intptyped arguments.
(gh-24888)
numpy.fft.helper made private#
numpy.fft.helper was renamed tonumpy.fft._helper to indicatethat it is a private submodule. All public functions exported by itshould be accessed fromnumpy.fft.
(gh-24945)
numpy.linalg.linalg made private#
numpy.linalg.linalg was renamed tonumpy.linalg._linalgto indicate that it is a private submodule. All public functionsexported by it should be accessed fromnumpy.linalg.
(gh-24946)
Out-of-bound axis not the same asaxis=None#
In some casesaxis=32 or for concatenate any large valuewas the same asaxis=None.Except forconcatenate this was deprecate.Any out of bound axis value will now error, make sure to useaxis=None.
(gh-25149)
Newcopy keyword meaning forarray andasarray constructors#
Nownumpy.array andnumpy.asarray support three values forcopy parameter:
None- A copy will only be made if it is necessary.True- Always make a copy.False- Never make a copy. If a copy is required aValueErroris raised.
The meaning ofFalse changed as it now raises an exception if a copy is needed.
(gh-25168)
The__array__ special method now takes acopy keyword argument.#
NumPy will passcopy to the__array__ special method in situations whereit would be set to a non-default value (e.g. in a call tonp.asarray(some_object,copy=False)). Currently, if anunexpected keyword argument error is raised after this, NumPy will print awarning and re-try without thecopy keyword argument. Implementations ofobjects implementing the__array__ protocol should accept acopy keywordargument with the same meaning as when passed tonumpy.array ornumpy.asarray.
(gh-25168)
Cleanup of initialization ofnumpy.dtype with strings with commas#
The interpretation of strings with commas is changed slightly, in that atrailing comma will now always create a structured dtype. E.g., wherepreviouslynp.dtype("i") andnp.dtype("i,") were treated as identical,nownp.dtype("i,") will create a structured dtype, with a singlefield. This is analogous tonp.dtype("i,i") creating a structured dtypewith two fields, and makes the behaviour consistent with that expected oftuples.
At the same time, the use of single number surrounded by parenthesis toindicate a sub-array shape, like innp.dtype("(2)i,"), is deprecated.Instead; one should usenp.dtype("(2,)i") ornp.dtype("2i").Eventually, using a number in parentheses will raise an exception, like is thecase for initializations without a comma, likenp.dtype("(2)i").
(gh-25434)
Change in how complex sign is calculated#
Following the array API standard, the complex sign is now calculated asz/|z| (instead of the rather less logical case where the sign ofthe real part was taken, unless the real part was zero, in which casethe sign of the imaginary part was returned). Like for real numbers,zero is returned ifz==0.
(gh-25441)
Return types of functions that returned a list of arrays#
Functions that returned a list of ndarrays have been changed to return a tupleof ndarrays instead. Returning tuples consistently whenever a sequence ofarrays is returned makes it easier for JIT compilers like Numba, as well as forstatic type checkers in some cases, to support these functions. Changedfunctions are:atleast_1d,atleast_2d,atleast_3d,broadcast_arrays,meshgrid,ogrid,histogramdd.
np.uniquereturn_inverse shape for multi-dimensional inputs#
When multi-dimensional inputs are passed tonp.unique withreturn_inverse=True,theunique_inverse output is now shaped such that the input can be reconstructeddirectly usingnp.take(unique,unique_inverse) whenaxis=None, andnp.take_along_axis(unique,unique_inverse,axis=axis) otherwise.
Note
This change was reverted in 2.0.1 except foraxis=None. The correctreconstruction is alwaysnp.take(unique,unique_inverse,axis=axis).When 2.0.0 needs to be supported, addunique_inverse.reshape(-1)to code.
any andall return booleans for object arrays#
Theany andall functions and methods now returnbooleans also for object arrays. Previously, they dida reduction which behaved like the Pythonor andand operators which evaluates to one of the arguments.You can usenp.logical_or.reduce andnp.logical_and.reduceto achieve the previous behavior.
(gh-25712)
np.can_cast cannot be called on Python int, float, or complex#
np.can_cast cannot be called with Python int, float, or complex instancesanymore. This is because NEP 50 means that the result ofcan_cast mustnot depend on the value passed in.Unfortunately, for Python scalars whether a cast should be considered"same_kind" or"safe" may depend on the context and value so thatthis is currently not implemented.In some cases, this means you may have to add a specific path for:iftype(obj)in(int,float,complex):....
(gh-26393)