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

Sorting should work for array scalars. #7051

Open
charris opened this issue Jan 18, 2016 · 9 comments
Open

Sorting should work for array scalars. #7051

charris opened this issue Jan 18, 2016 · 9 comments

Comments

@charris
Copy link
Member

charris commented Jan 18, 2016

Just return the array, don't error.


In [11]: a.ndim
Out[11]: 0

In [12]: a.sort()

ValueError                                Traceback (most recent call last)
<ipython-input-12-e7eb8b51a6fa> in <module>()
----> 1 a.sort()

ValueError: axis(=-1) out of bounds

@tushar-rishav
Copy link
Contributor

@charris Checking a.ndim for zero before calling a.sort here avoids the ValueError while computing np.sort(a) for array scalars. I've this tried this and it worked

if not a.ndim:
    a = asanyarray([a])
a.sort(axis, kind, order)
return a

Probably, doing just that won't help because we'll still get ValueError in case of a.sort(). I noticed that behind the curtain a.sort() calls PyArray_Sort which takes PyArrayObject instance , axis and order . Unfortunately, I couldn't get much insight about how ndarray.sort() has been implemented here.

#define PyArray_Sort  (*(int (*)(PyArrayObject *, int, NPY_SORTKIND))  PyArray_API[129])

As I am a beginner I might be missing something here. Could you please let me know where PyArrayObject has been declared?
Thanks in advance!

@charris
Copy link
Member Author

charris commented Jan 19, 2016

The base sort is an array method, so that is what we want fixed.

@seberg
Copy link
Member

seberg commented Jan 19, 2016

I agree that it is silly that this exists and does not work, but on the other hand the default for the sort is axis=-1, which is correctly an error for 0-d arrays unlike most similar methods which use axis=None (unsupported currently in sort) where this is no problem.
I would be +1 to allow arr.sort(None), which would also make sense for 0-d. But I don't feel just removing the error is more canonical, so I am slightly against it unless there is at least some use case.

@charris
Copy link
Member Author

charris commented Jan 20, 2016

I ran into this because of an argument that could be either a list or a scalar and I wanted to preserve that distinction while also sorting the result of asarray. So in that case it would avoid special casing.

@jaimefrio
Copy link
Member

Do we want to fix this for the Python API only, or for PyArray_Sort as well? Should array scalars suceed regardless of the value of axis, or should they only do for axis=-1?

On a related note, is this behavior correct, or should the return also be an array scalar?

>>> np.argsort(np.array(8))
array([0])

@njsmith
Copy link
Member

njsmith commented Jan 20, 2016

I'm not sure that this does make sense... sorting needs a dimension to
operate on, doesn't it? You could flatten, but sort is inplace so that
doesn't make much sense either...
On Jan 19, 2016 4:34 PM, "Jaime" notifications@github.com wrote:

Do we want to fix this for the Python API only, or for PyArray_Sort as
well? Should array scalars suceed regardless of the value of axis, or
should they only do for axis=-1?

On a related note, is this behavior correct, or should the return also be
an array scalar?

np.argsort(np.array(8))
array([0])


Reply to this email directly or view it on GitHub
#7051 (comment).

@charris
Copy link
Member Author

charris commented Jan 20, 2016

@njsmith Array scalars can be operated on inplace. I guess I'm looking at 0-D arrays as containers rather than scalars even if they can't be indexed with integers.

@charris
Copy link
Member Author

charris commented Jan 20, 2016

Mind, i'd just like the operation to be a NOP rather than an error.

@njsmith
Copy link
Member

njsmith commented Jan 20, 2016

I'm just saying that sorting is an intrinsically one-dimensional operation
(it feels like to me). So I can see an argument for zerod_array.sort()
being equivalent to zerod_array.resize((1,)) (with the sort implicitly
doing an in-place flatten to get something one-dimensional and sortable,
and then sorting it, which would be a no-op because there's only one
element). But zerod_array.sort() doesn't make sense to me as a no-op that
leaves you with a zero-d output.
On Jan 19, 2016 5:07 PM, "Charles Harris" notifications@github.com wrote:

Mind, i'd just like the operation to be a NOP rather than an error.


Reply to this email directly or view it on GitHub
#7051 (comment).

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