Returns True if the type of element is a scalar type.
Input argument, can be of any type and shape.
True if element is a scalar type, False if it is not.
See also
ndimGet the number of dimensions of an array
If you need a stricter way to identify a numerical scalar, use isinstance(x, numbers.Number), as that returns False for most non-numerical elements such as strings.
In most 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 |
|
|
|---|---|---|
PEP 3141 numeric objects (including builtins) |
|
|
builtin string and buffer objects |
|
|
other builtin objects, like |
|
|
third-party objects like |
|
|
zero-dimensional numpy arrays |
|
|
other numpy arrays |
|
|
|
|
|
>>> import numpy as np
>>> np.isscalar(3.1) True
>>> np.isscalar(np.array(3.1)) False
>>> np.isscalar([3.1]) False
>>> np.isscalar(False) True
>>> np.isscalar('numpy')
True
NumPy supports PEP 3141 numbers:
>>> from fractions import Fraction >>> np.isscalar(Fraction(5, 17)) True >>> from numbers import Number >>> np.isscalar(Number()) True
© 2005–2024 NumPy Developers
Licensed under the 3-clause BSD License.
https://numpy.org/doc/2.4/reference/generated/numpy.isscalar.html