NumPy 2.5.0rc1:移除 distutils,弃用多项 API
v2.5.0rc1 (June 2, 2026)
NumPy 是 Python 科学计算的核心库,本次 RC 版本包含大量弃用和破坏性变更,直接影响现有代码的兼容性。建议开发者关注弃用列表,提前迁移代码,尤其是设置 dtype/shape 属性和 numpy.fix 等常用功能。
NumPy 2.5.0 Release Notes
Numpy 2.5.0 is a transitional release. It drops support for Python 3.11,
marking the end of distutils, and expires a large number of deprecations made
in the 2.0.x release. It also improves free threading and brings sorting into
compliance with the array-api standard with the addition of descending sorts.
Python 3.15 will be supported when it is released.
This release supports Python versions 3.12-3.14.
Highlights
- Distutils has been removed,
- Many expired deprecations, see below,
- Many new deprecations, see below,
- Many static typing improvements.
- Improved support for free threading,
- Support for descending sorts,
See New Features below for other additions.
Deprecations
- numpy.char.chararray is deprecated. Use an ndarray with a string or bytes dtype instead.
- (gh-30605)
- numpy.take now correctly checks if the result can be cast to the provided
- out=out under the same-kind rule. A DeprecationWarning is given now
- when this check fails. Previously, take incorrectly checked if out
- could be cast to the result (the wrong direction). This deprecation also
- affects compress and possibly other functions. (Future versions of NumPy
- may tighten the casting check further.)
- (gh-30615)
- The numpy.char.[as]array functions are deprecated. Use an
- numpy.[as]array with a string or bytes dtype instead.
- (gh-30802)
- Setting the dtype attribute is deprecated because mutating an array is unsafe
- if an array is shared, especially by multiple threads. As an alternative,
- you can create a view with a new dtype via array.view(dtype=new_dtype).
- (gh-29244)
- Setting the shape attribute is deprecated because mutating an array is
- unsafe if an array is shared, especially by multiple threads. As an
- alternative, you can create a new view via np.reshape or
- np.ndarray.reshape. For example: x = np.arange(15); x = np.reshape(x, (3, 5)).
- To ensure no copy is made from the data, one can use np.reshape(..., copy=False).
- While setting the shape on an array is discouraged, for cases where it is
- difficult to work around, e.g., in __array_finalize__, it is possible
- with the private method np.ndarray._set_shape.
- (gh-29536)
- Using the generic unit in numpy.timedelta64 is deprecated since this
- can lead to unexpected behavior such as non-transitive comparison, see
- gh-28287 for details. As
- an alternative, specify an explicit unit such as 's' (seconds) or 'D'
- (days) when constructing numpy.timedelta64. Due to this change, operations
- that implicitly rely on the generic unit are also deprecated. For
- example:
arr = np.array([1, 2, 3], dtype="m8[s]")
# `1` is implicitly converted to generic timedelta64
arr + 1- (gh-29619)
- Resizing a Numpy array in place is deprecated since mutating an array is
- unsafe if an array is shared, especially by multiple threads. As an
- alternative, you can create a resized array via np.resize.
- (gh-30181)
- numpy.fix is deprecated, use numpy.trunc instead. It is faster and
- follows the Array API standard. Both functions provide identical
- functionality: rounding array elements towards zero.
- (gh-30644)
- numpy.ma.round_ is deprecated. numpy.ma.round can be used as a
- replacement.
- (gh-30738)
- numpy.typename is deprecated because the names returned by it were
- outdated and inconsistent. numpy.dtype.name can be used as a
- replacement.
- (gh-30774)
- Inputs other than integers are deprecated for numpy.triu_indices and
- numpy.tril_indices. Non-integer values for the M, k and N
- parameters of numpy.tri are deprecated. Non-integer values for the k
- parameter of both numpy.tril_indices_from and numpy.triu_indices_from
- are deprecated.
- (gh-30869)
- Deprecations in custom dtype property and __array_finalize__.
- Previously arr.view(dtype=new_dtype) called arr.dtype = new_dtype
- also for subclasses, i.e., the attribute setting. That path is now
- deprecated and refined, meaning that even subclasses that do not see this
- DeprecationWarning may wish to update their code.
- A subclass that does any dtype specific logic (i.e. verifying the dtype
- in __array_finalize__ or has a dtype property) should now:
- Set _set_dtype = None in which case arr.view(dtype=new_dtype)
- will call __array_finalize__ with the new dtype, ensuring that
- any validation __array_finalize__ will run is done.
- Or, for a quick fix, define _set_dtype as a function (calling
- ndarray._set_dtype() to avoid DeprecationWarnings.
- (Future versions might migrate towards the _set_dtype = None path.)
- Ideally, follow NumPy's deprecation to prevent dtype mutation by users.
- The use of ndarray._set_dtype() may be necessary for some subclass
- finalization patterns, but should otherwise be avoided.
- (gh-31293)
Expired deprecations
- numpy.distutils has been removed
- (gh-30340)
- Passing None as dtype to np.finfo will now raise a TypeError
- (deprecated since 1.25)
- (gh-30460)
- numpy.cross no longer supports 2-dimensional vectors.
- (Deprecated since 2.0)
- (gh-30461)
- numpy._core.numerictypes.maximum_sctype has been removed.
- (deprecated since 2.0)
- (gh-30462)
- numpy.row_stack has been removed in favor of numpy.vstack.
- (deprecated since 2.0)
- (gh-30463)
- get_array_wrap has been removed.
- (deprecated since 2.0)
- (gh-30463)
- recfromtxt and recfromcsv have been removed from numpy.lib._npyio
- in favor of numpy.genfromtxt.
- (deprecated since 2.0)
- (gh-30467)
- The numpy.chararray re-export of numpy.char.chararray has been removed.
- (deprecated since 2.0)
- (gh-30604)
- bincount now raises a TypeError for non-integer inputs.
- (deprecated since 2.1)
- (gh-30610)
- The numpy.lib.math alias for the standard library math module has
- been removed.
- (deprecated since 1.25)
- (gh-30612)
- Data type alias 'a' was removed in favor of 'S'.
- (deprecated since 2.0)
- (gh-30613)
- _add_newdoc_ufunc(ufunc, newdoc) has been removed in favor of
- ufunc.__doc__ = newdoc.
- (deprecated since 2.2)
- (gh-30614)
Compatibility notes
linalg.eig and linalg.eigvals now always return complex arrays
Previously, the return values depended on whether the eigenvalues happen to lie
on the real line (which, for a general, non-symmetric matrix, is not
guaranteed).
This change makes consistent what was a value-dependent result. To retain the
previous behavior, do:
w = eigvals(a)
if np.any(w.imag == 0): # this is what NumPy used to do
w = w.realIf your matrix is symmetrix/hermitian, use eigh and eigvalsh instead of
eig and eigvals. These are guaranteed to return real values. A common
case is covariance matrices, which are symmetric and positive definite by
construction.
(gh-30411)
MSVC support
NumPy now requires minimum MSVC 19.35 toolchain version on Windows platforms.
This corresponds to Visual Studio 2022 version 17.5 Preview 2 or newer.
(gh-30489)
Cython support
NumPy's Cython headers (accessed via cimport numpy) now require Cython 3.0
or newer to build. If you try to compile a project that depends on NumPy's
Cython headers using Cython 0.29 or older, you will see a message like this:
Error compiling Cython file:
------------------------------------------------------------
...
# versions.
#
# See __init__.cython-30.pxd for the real Cython header
#
DEF err = int('Build aborted: the NumPy Cython headers require Cython 3.0.0 or newer.')
------------------------------------------------------------
/path/to/site-packages/numpy/__init__.pxd:11:13: Error in compile-time expression:
ValueError: invalid literal for int() with base 10:
'Build aborted: the NumPy Cython headers require Cython 3.0.0 or newer.'Note that the invalid integer is not a bug in NumPy - we are intentionally
generating this error to avoid triggering a more obscure error later in the
build when an older Cython version tries to use a Cython feature that was not
更进一步:量化金融体系
看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力