-
Notifications
You must be signed in to change notification settings - Fork 46
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
RFC: item()
to return scalar for arrays with exactly 1 element.
#815
Comments
This was discussed in #710 , along with the more general |
|
This is a minor convenience method though, since def as_pyscalar(x):
if xp.isdtype(x, 'real floating'):
return float(x)
elif xp.isdtype(x, 'complex floating'):
return complex(x)
elif xp.isdtype(x, 'integral'):
return int(x)
elif xp.isdtype(x, 'bool'):
return bool(x)
else:
# raise error, or handle custom/non-standard dtypes if desired Static typing of such a function, and of |
>>> np.array([1]).item()
1 |
We discussed this in a call today, and concluded that this fell into a bucket of functionality that is useful, but also easy to implement on top of what's already in the standard. In addition, there are problems with trying to add this: a Outcome:
|
On a very fundamental level, I believe From a mathematical PoV, |
Is this usage guaranteed? If so, should it be added somewhere to the specification? I looked for it here. FWIW I also like the |
Yes, |
Examples:
numpy.ndarray.item
torch.Tensor.item
pandas.Series.item
pandas.Index.item
polars.Series.item
xarray.DataArray.item
Demo:
Currently, only
torch
fails, because it raisesRuntimeError
instead ofValueError
.The text was updated successfully, but these errors were encountered: