diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2018-09-08 12:05:57 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-08 12:05:57 -0500 |
commit | 639004c38b96966fb411d5864323278ce6d07aba (patch) | |
tree | a79566961b97279b4817166d29060c56445dab56 /numpy/core/numeric.py | |
parent | 4913a62a074cf10a82b3bd4fd61a46096590fab3 (diff) | |
parent | 547c74767d00e69a1385a588b6ed00b67bf87993 (diff) | |
download | numpy-639004c38b96966fb411d5864323278ce6d07aba.tar.gz |
Merge pull request #11882 from eric-wieser/isscalar-docs
DOC: Recommend the use of `np.ndim` over `np.isscalar`, and explain the differences
Diffstat (limited to 'numpy/core/numeric.py')
-rw-r--r-- | numpy/core/numeric.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index 1b4818b76..2e46057ac 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -1942,10 +1942,46 @@ def isscalar(num): val : bool True if `num` is a scalar type, False if it is not. + See Also + -------- + ndim : Get the number of dimensions of an array + + Notes + ----- + In almost all cases ``np.ndim(x) == 0`` should be used instead of this + function, as that will also return true for 0d arrays. This is how + numpy overloads functions in the style of the ``dx`` arguments to `gradient` + and the ``bins`` argument to `histogram`. Some key differences: + + +--------------------------------------+---------------+-------------------+ + | x |``isscalar(x)``|``np.ndim(x) == 0``| + +======================================+===============+===================+ + | PEP 3141 numeric objects (including | ``True`` | ``True`` | + | builtins) | | | + +--------------------------------------+---------------+-------------------+ + | builtin string and buffer objects | ``True`` | ``True`` | + +--------------------------------------+---------------+-------------------+ + | other builtin objects, like | ``False`` | ``True`` | + | `pathlib.Path`, `Exception`, | | | + | the result of `re.compile` | | | + +--------------------------------------+---------------+-------------------+ + | third-party objects like | ``False`` | ``True`` | + | `matplotlib.figure.Figure` | | | + +--------------------------------------+---------------+-------------------+ + | zero-dimensional numpy arrays | ``False`` | ``True`` | + +--------------------------------------+---------------+-------------------+ + | other numpy arrays | ``False`` | ``False`` | + +--------------------------------------+---------------+-------------------+ + | `list`, `tuple`, and other sequence | ``False`` | ``False`` | + | objects | | | + +--------------------------------------+---------------+-------------------+ + Examples -------- >>> np.isscalar(3.1) True + >>> np.isscalar(np.array(3.1)) + False >>> np.isscalar([3.1]) False >>> np.isscalar(False) |