@@ -5,7 +5,33 @@ NumPy 2.0 migration guide
5
5
*************************
6
6
7
7
This document contains a set of instructions on how to update your code to
8
- work with Numpy 2.0.
8
+ work with NumPy 2.0. It covers changes in NumPy's Python and C APIs.
9
+
10
+ .. note ::
11
+
12
+ Note that NumPy 2.0 also breaks binary compatibility - if you are
13
+ distributing binaries for a Python package that depends on NumPy's C API,
14
+ please see :ref: `numpy-2-abi-handling `.
15
+
16
+
17
+
18
+ Ruff plugin
19
+ ===========
20
+
21
+ Many of the changes covered in the 2.0 release notes and in this migration
22
+ guide can be automatically adapted to in downstream code with a dedicated
23
+ `Ruff <https://docs.astral.sh/ruff/ >`__ rule, namely rule
24
+ `NPY201 <https://docs.astral.sh/ruff/rules/numpy2-deprecation/ >`__.
25
+
26
+ You should install ``ruff>=0.2.0 `` and add the ``NPY201 `` rule to your
27
+ ``pyproject.toml ``::
28
+
29
+ [tool.ruff.lint]
30
+ select = ["NPY201"]
31
+
32
+ You can also apply the NumPy 2.0 rule directly from the command line::
33
+
34
+ $ ruff check path/to/code/ --select NPY201
9
35
10
36
11
37
.. _migration_promotion_changes :
@@ -189,8 +215,8 @@ The underlying type remains a struct under C++ (all of the above still remains
189
215
valid).
190
216
191
217
192
- Namespace changes
193
- =================
218
+ Changes to namespaces
219
+ =====================
194
220
195
221
In NumPy 2.0 certain functions, modules, and constants were moved or removed
196
222
to make the NumPy namespace more user-friendly by removing unnecessary or
@@ -299,8 +325,8 @@ downstream libraries we don't provide any information on how to replace them:
299
325
``MAY_SHARE_BOUNDS ``]
300
326
301
327
302
- Lib namespace
303
- -------------
328
+ numpy.lib namespace
329
+ -------------------
304
330
305
331
Most of the functions available within ``np.lib `` are also present in the main
306
332
namespace, which is their primary location. To make it unambiguous how to access each
@@ -322,10 +348,10 @@ the main namespace, then you're using a private member. You should either use th
322
348
API or, in case it's infeasible, reach out to us with a request to restore the removed entry.
323
349
324
350
325
- Core namespace
326
- --------------
351
+ numpy.core namespace
352
+ --------------------
327
353
328
- ``np.core `` namespace is now officially private and has been renamed to ``np._core ``.
354
+ The ``np.core `` namespace is now officially private and has been renamed to ``np._core ``.
329
355
The user should never fetch members from the ``_core `` directly - instead the main
330
356
namespace should be used to access the attribute in question. The layout of the ``_core ``
331
357
module might change in the future without notice, contrary to public modules which adhere
@@ -334,8 +360,8 @@ then you should either use the existing API or, in case it's infeasible, reach o
334
360
with a request to restore the removed entry.
335
361
336
362
337
- ndarray and scalar namespace
338
- ----------------------------
363
+ ndarray and scalar methods
364
+ --------------------------
339
365
340
366
A few methods from ``np.ndarray `` and ``np.generic `` scalar classes have been removed.
341
367
The table below provides replacements for the removed members:
@@ -350,32 +376,18 @@ setitem Use ``arr[index] = value`` instead.
350
376
====================== ========================================================
351
377
352
378
353
- Strings namespace
354
- -----------------
379
+ numpy.strings namespace
380
+ -----------------------
355
381
356
- A new `` np .strings` ` namespace has been created, where most of the string
357
- operations are implemented as ufuncs. The old `` np .char` ` namespace still is
382
+ A new `numpy .strings ` namespace has been created, where most of the string
383
+ operations are implemented as ufuncs. The old `numpy .char ` namespace still is
358
384
available, and, wherever possible, uses the new ufuncs for greater performance.
359
- We recommend using the ``np.strings `` methods going forward. The ``np.char ``
360
- namespace may be deprecated in the future.
361
-
362
-
363
- Ruff plugin
364
- -----------
385
+ We recommend using the `~numpy.strings ` functions going forward. The
386
+ `~numpy.char ` namespace may be deprecated in the future.
365
387
366
- All the changes that we covered in the previous sections can be automatically applied
367
- to the codebase with the dedicated Ruff rule,
368
- `NPY201 <https://docs.astral.sh/ruff/rules/numpy2-deprecation/ >`_.
369
388
370
- You should install Ruff, version ``0.2.0 `` or above, and add the ``NPY201 `` rule to
371
- your ``pyproject.toml ``::
372
-
373
- [tool.ruff.lint]
374
- select = ["NPY201"]
375
-
376
- You can run NumPy 2.0 rule also directly from the command line::
377
-
378
- $ ruff check path/to/code/ --select NPY201
389
+ Other changes
390
+ =============
379
391
380
392
381
393
Note about pickled files
@@ -384,3 +396,23 @@ Note about pickled files
384
396
NumPy 2.0 is designed to load pickle files created with NumPy 1.26,
385
397
and vice versa. For versions 1.25 and earlier loading NumPy 2.0
386
398
pickle file will throw an exception.
399
+
400
+
401
+ Adapting to changes in the ``copy `` keyword
402
+ -------------------------------------------
403
+
404
+ The :ref: `copy keyword behavior changes <copy-keyword-changes-2.0 >` in
405
+ `~numpy.asarray `, `~numpy.array ` and `ndarray.__array__
406
+ <numpy.ndarray.__array__> ` may require these changes:
407
+
408
+ 1. Code using ``np.array(..., copy=False) `` can in most cases be changed to
409
+ ``np.asarray(...) ``. Older code tended to use ``np.array `` like this because
410
+ it had less overhead than the default ``np.asarray `` copy-if-needed
411
+ behavior. This is no longer true, and ``np.asarray `` is the preferred function.
412
+ 2. For code that explicitly needs to pass ``None ``/``False `` meaning "copy if
413
+ needed" in a way that's compatible with NumPy 1.x and 2.x, see
414
+ `scipy#20172 <https://github.com/scipy/scipy/pull/20172 >`__ for an example
415
+ of how to do so.
416
+ 3. For any ``__array__ `` method on a non-NumPy array-like object, a
417
+ ``copy=None `` keyword can be added to the signature - this will work with
418
+ older NumPy versions as well.
0 commit comments