DataArray.argsort() shouldn't return a DataArray

63 views
Skip to first unread message

Seth P

unread,
Oct 11, 2017, 5:37:41 PM10/11/17
to xarray
I remember pointing out this same semantic error in Pandas, as banging my head against a wall that no one seemed to get it.

Consider the following:


In [1]: import xarray as xr

In [2]: xr.DataArray([5,4,3,2,8,-1], dims=('foo',), coords={'foo': ['a', 'b', 'c', 'd', 'e', 'f']}).argsort()
Out[2]:
<xarray.DataArray (foo: 6)>
array
([5, 3, 2, 1, 0, 4], dtype=int64)
Coordinates:
 
* foo      (foo) <U1 'a' 'b' 'c' 'd' 'e' 'f'


The resulting values [5, 3, 2, 1, 0, 4] are indices into the original array, and it does not make sense to label them with the foo coordinates 'a', 'b', ..., 'f'. They should just be left as an ndarray.

It *would* make sense to have a function that returned a similarly shaped DataArray whose values are the ordered location of the corresponding values, i.e. something like this:

In [3]: xr.DataArray([5,4,3,2,8,-1], dims=('foo',), coords={'foo': ['a', 'b', 'c', 'd', 'e', 'f']}).rank()  # hypothetical
Out[3]:
<xarray.DataArray (foo: 6)>
array
([4, 3, 2, 1, 5, 0])
Coordinates:
 
* foo      (foo) <U1 'a' 'b' 'c' 'd' 'e' 'f'


Message has been deleted

Seth P

unread,
Oct 12, 2017, 5:31:10 PM10/12/17
to xarray
By the way, note that the hypothetical da.rank() function I propose is equivalent to the existing da.argsort().argsort() (i.e. repeating argsort()
twice).

Stephan Hoyer

unread,
Oct 12, 2017, 5:45:12 PM10/12/17
to xarray
This is a good sort of topic to bring up in a GitHub issue for hashing out the design.

The original argsort() was not very carefully thought out, so I am not surprised that there are natural improvements. A rank() method (like pandas) also sounds like something that would be welcome. 

On Thu, Oct 12, 2017 at 2:31 PM Seth P <set...@outlook.com> wrote:

Seth Padowitz

unread,
Oct 16, 2017, 9:54:14 PM10/16/17
to xar...@googlegroups.com
Yes, I realize that. But the point is that it makes. I sense to wrap that ndarray into a DataArray.

On Oct 11, 2017, at 10:27 PM, Spencer Hill <spence...@gmail.com> wrote:

You can access the underlying ndarray via `.values`

--
You received this message because you are subscribed to a topic in the Google Groups "xarray" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/xarray/wsxeiIPLhgM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to xarray+un...@googlegroups.com.
To post to this group, send email to xar...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/xarray/bf602e58-1d8e-4568-923e-5b4adcfec2e0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Seth P

unread,
Oct 17, 2017, 2:36:45 PM10/17/17
to xarray
Reply all
Reply to author
Forward
0 new messages