Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DEP: deprecate scalar conversions for arrays with ndim > 0 (#10615)
This PR reflects some of the progress achieved in issue #10404 and is used to asses the impact of the changes. With the changes in this PR, `float(numpy.array([1.0])` now gives a warning; likewise some other things: ```python import numpy a = numpy.random.rand(10, 1) a[0] = numpy.array([1.0]) # okay a[0] = numpy.array(1.0) # okay a[0] = 1.0 # okay b = numpy.random.rand(10) b[0] = numpy.array([1.0]) # ValueError: setting an array element with a sequence. b[0, ...] = numpy.array([1.0]) # okay b[0] = numpy.array(1.0) # okay b[0] = 1.0 # okay ``` This aligns the behavior of numpy arrays with that of lists: ```python float([3.14]) ``` ``` TypeError: float() argument must be a string or a number, not 'list' ``` ```python import numpy as np a = np.random.rand(5) a[0] = [3.14] ``` ``` ValueError: setting an array element with a sequence. ``` Fixes #10404.
- Loading branch information
Showing
8 changed files
with
83 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
Only ndim-0 arrays are treated as scalars | ||
----------------------------------------- | ||
NumPy used to treat all arrays of size 1 (e.g., ``np.array([3.14])``) as scalars. | ||
In the future, this will be limited to arrays of ndim 0 (e.g., ``np.array(3.14)``). | ||
The following expressions will report a deprecation warning: | ||
|
||
.. code-block:: python | ||
a = np.array([3.14]) | ||
float(a) # better: a[0] to get the numpy.float or a.item() | ||
b = np.array([[3.14]]) | ||
c = numpy.random.rand(10) | ||
c[0] = b # better: c[0] = b[0, 0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters