-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ak.Array and ak.from_numpy should not accept zero-dimensional arrays #1057
Comments
@jpivarski do you want to ban this at the |
Yeah, it should be banned at the
Both zero-dimensional arrays and scalars have a >>> np.array(3.14).shape
()
>>> np.float64(3.14).shape
() But scalars are not instances of >>> isinstance(np.array(3.14), np.ndarray)
True
>>> isinstance(np.float64(3.14), np.ndarray)
False If >>> something = np.array(3.14)
>>> isinstance(something, np.ndarray)
True
>>> # Below is the check that to_layout could do...
>>> if len(getattr(something, "shape", (None,))) == 0 and hasattr(something, "dtype"):
... something = something.dtype.type(something)
...
>>> isinstance(something, np.ndarray)
False |
As of now, this is banned at the >>> ak.Array(np.array(123))
TypeError: Encountered a scalar (ndarray), but scalar conversion/promotion is disabled
This error occurred while calling
ak.to_layout(
numpy.ndarray(123)
allow_record = False
regulararray = False
primitive_policy = 'error'
)
>>> ak.from_numpy(np.array(123))
<Array [123] type='1 * int64'>
>>> ak.to_layout(np.array(123))
<NumpyArray dtype='int64' len='1'>[123]</NumpyArray> It's relevant for Ragged because Ragged has to support zero-dimensional arrays. (The |
... because they're not iterable. This was raised in an issue by @bfis:
Elsewhere, we consider NumPy scalars (e.g.
np.float32(3.14)
) to be equivalent to zero-dimensional arrays (e.g.np.array(3.14, np.float32)
), but this is interpreting the zero-dimensional array as a length-1 array.ak.Array
andak.from_numpy
do not accept scalars:So they should not accept zero-dimensional arrays, for consistency. In other words, not this:
Originally posted by @jpivarski in #1055 (comment), with @bfis's response in #1055 (comment)
The text was updated successfully, but these errors were encountered: