[Numpy-discussion] Improving Complex Comparison/Ordering in Numpy

Sebastian Berg sebastian at sipsolutions.net
Wed Jul 1 21:44:04 EDT 2020


On Wed, 2020-07-01 at 12:48 -0700, Stephan Hoyer wrote:
> On Wed, Jul 1, 2020 at 12:23 PM Sebastian Berg <
> sebastian at sipsolutions.net>
> wrote:
> 
> > This is a WIP, but allows nicely to try out how the new API
> > could/should look like, and see the potential impact to code.  The
> > current choice is for:
> > 
> >     np.sort(arr, keys=(arr.real, arr.image))
> > 
> > for example.  `keys` is like the `key` argument to pythons sorts,
> > but
> > unlike python sorts is not passed a function but rather a sequence
> > of
> > arrays.
> > 
> > Alternative spellings could be `by=...`? Or maybe someone has a
> > different API idea.
> > 
> 
> I really like the look of np.sort(arr, by=(arr.real, arr.image)).
> - This avoids adding an extra function sortby into NumPy's API. The
> default
> behavior (by=None) would of course be to sort by the arrays being
> sorted,
> so it's backwards compatible.
> - Calling the new argument "by" instead of "key" avoids confusion
> with the
> behavior of Python's sort/sorted (which take functions instead of
> sequences).


I just noticed that `DataFrame.sort_values()` uses `by=...` with a list
of column names.  However, I guess that is fairly compatible with this
usage.

- Sebastan


> The combination of lexsort() and take_along_axis() makes it possible
> to
> achieve this behavior currently, but it is definitely less clear than
> a
> single function call.
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20200701/2c59ed52/attachment.sig>


More information about the NumPy-Discussion mailing list