Skip to content
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

asarray array disagrees with isscalar about what is a scalar (at least for sets) #14396

Open
allComputableThings opened this issue Aug 29, 2019 · 9 comments

Comments

@allComputableThings
Copy link

allComputableThings commented Aug 29, 2019

np.asarray believes sets are scalar:

>>> np.asarray(1).shape
()   # scalar
>>> np.asarray({1,2,3}).shape
()  # scalar

np.isscalar does not:

>>> np.isscalar(1)
True
>>> np.isscalar({1,2,3})
False

I'd argues that asarray should always returns an scalar if a scalar is passed, and a non-scalar if a non-scalar is passed. (to be consistent with its treatment when a plain number is passed in).

Related to #8538 ?

@seberg
Copy link
Member

seberg commented Aug 29, 2019

isscalar is a pretty buggy/limited function I guess. np.ndim() == 0 would give you a better result probably (but will coerce to array as an intermediate if necessary).

Yes, that issue (and I am pretty sure one more), is basically a duplicate. isscalar uses its full own set of rules to decide what a scalar is, and that just cannot work, I guess.

@eric-wieser
Copy link
Member

Perhaps we should try to deprecate isscalar - we have a very small number of internal uses left.

@rgommers
Copy link
Member

That's going to be very noisy, there's a large amount of usage in SciPy, and I checked Dask, scikit-image and scikit-image, that turned up 5-20 usages per package.

So if we really want to deprecate isscalar, then I'd rather do it longer-term (say in 1-2 years) and first push out fixes to all those libraries. And given the widespread usage, removal before a NumPy 2.0 seems to be a bad idea.

@allComputableThings
Copy link
Author

Does isscalar need to be deprecated?
It seems like a useful function -- its just that numpy doesn't have consistent rules for scalarness. My use was to decide whether to call f(x): float or g(x): floats depends on whether x was a collection. f for numbers, g for things that can be asarray.

I'd say if anything should change it might be that np.asarray(set) should return a non-scalar array.

What do others think should happen if I ask for np.asarray(set) ?

@seberg
Copy link
Member

seberg commented Aug 30, 2019

The careful consensus right now is probably that np.asarray(set) should just raise an error, because returning np.asarray(list(set)) may be a bit strange, but returning a 0D array is just weird/unexpected. To be honest, my gut feeling is that it might be the best way to deprecate it. Sure, make it a super long deprecation. However, we may need a good replacement before we do so, and I am not sure np.ndim(x) == 0 is one.

EDIT: To be clear in the error scenario np.asarray(set, dtype=object) would still return a 0D array probably.

@allComputableThings
Copy link
Author

allComputableThings commented Aug 30, 2019 via email

@Aa88s

This comment has been minimized.

@Aa88s

This comment has been minimized.

@eric-wieser

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants