Feature request: sorted() methods on everything #9816

Closed
brandon-rhodes opened this Issue Apr 5, 2015 · 7 comments

Comments

Projects
None yet
5 participants
Contributor

brandon-rhodes commented Apr 5, 2015

It would make Pandas easier to teach, easier to learn, and easier to use if the sorting behavior were the same between series and dataframes. But the existing order() and sort() methods are locked into their old behaviors by all of the code that already depends on them.

But a new sorted() method could bring symmetry between series and dataframes for code written from now on:

Series.sorted()      =>  same as existing Series.order()
DataFrame.sorted()   =>  same as existing DataFrame.sort()

Having this new pair of methods with identical conventions, where possible, would solve several different problems that learners have with Pandas today:

  • In Pandas, nearly all methods return a new object by default instead of doing modification in-place, but learners discover that Series.sort() is a special case.
  • In Python, a sort() method traditionally returns None and does an in-place sort, but learners have to discover that DataFrame.sort() violates this convention in order to match the behavior of the rest of Pandas.
  • The new-object sorter for series objects is Series.order() which is very difficult to discover, as nothing else in the Python ecosystem is named order(), and since one would normally expect an order() method to tell you the order (ascending? descending? none?) instead of imposing a new order.
  • The standard Python name for a sort that returns a new object is sorted(), per the universally loved Python built-in, but learners cannot transfer this knowledge to Pandas, where that concept exists but under the two different names Series.order() and DataFrame.sort().

Yes, the ed at the end of sorted() would be one character longer than order() and two characters longer than the current practice of df.sort(). But, on balance, I think that most programmers would happily cede two characters in order to be able to use the same method name when they are flipping code between handling series and handling dataframes, and happy to have the option of using the standard Python name for the concept of a non-in-place sort.

I suspect that deprecating the old names would be overly disruptive at this point, and they could probably live alongside the new sorted() methods without much trouble — new documentation could adopt the new, consistent terminology where possible, if the Pandas developers did not want to disrupt current users of the old inconsistent names.

Contributor

jreback commented Apr 5, 2015

see #8239 for much of the same discussion

this would actually be a nice soln as changing the existing behavior or order/sort is back incompatible
but s new method would solve this problem and we could easily deprecate the original methods.

pull requested are welcome!

jreback added this to the 0.17.0 milestone Apr 5, 2015

Contributor

brandon-rhodes commented Apr 5, 2015

Thanks for the vote of interest — I will look for the Pandas team at the PyCon sprints :)

Contributor

jreback commented Apr 5, 2015

awesome! this would be amazing to do then!

Member

shoyer commented Apr 5, 2015

Agreed, this is a nice solution! 👍

jreback changed the title from Feature request: `sorted()` methods on everything to Feature request: sorted() methods on everything Apr 5, 2015

From me a 👍 as well!

But, I think there are some other aspects of the interface that needs discussion (as seen in #8239): how to specify to sort on a certain column, or column/index combination, default of sorting on index or values, ...
But in any case: a good proposal to deal with the possible back compat problems. Now finding a good interface.

Is there a reason that we can't just add an order method to DataFrame that does the same as what order does for Series? What exactly are the different capabilities that each method provides that we want to keep?

Contributor

jreback commented Apr 26, 2015

order is actually an odd term (from R I believe) and sort/sorted is more pythonic

the intention would be to replicate sort for DataFrame and order for Series
iow the non-in place behavior (as sort for a Series is in place: this came from numpy.sort originally which is in place)

@jreback jreback added a commit to jreback/pandas that referenced this issue Aug 18, 2015

@jreback jreback ENH/DEPR: add .sorted() method for API consistency, #9816, #8239
DEPR: remove of na_last from Series.order/Series.sort, xref #5231
13d2d71

jreback closed this in #10726 Aug 21, 2015

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