EDIT: this issue started as a technical discussion about the Categorical._values_for_argsort/factorize, but in the end it is an API question about how non-ordered Categorical values should sort: simply sort the codes (but if the categories are not sorted, it doesn't make the end result sorted), or sort by the categories (like what you would get if you sort the materialized values)?
For that discussion, jump to #33245 (comment)
Currently, rank follows the order by value, sort_values/argsort the order by code.
Some links:
Original:
The docstrings for these two methods say that values_for_foo should preserve order, but for non-ordered Categoricals, the current implementation using self.codes is not order-preserving. Is this a problem cc @jorisvandenbossche ?
Categorical.values_for_rank exists pretty much to solve this problem for rank and value_counts. We have a special case in core.algorithms using values_for_rank that we should ideally avoid.
EDIT: this issue started as a technical discussion about the
Categorical._values_for_argsort/factorize, but in the end it is an API question about how non-ordered Categorical values should sort: simply sort the codes (but if the categories are not sorted, it doesn't make the end result sorted), or sort by the categories (like what you would get if you sort the materialized values)?For that discussion, jump to #33245 (comment)
Currently,
rankfollows the order by value,sort_values/argsortthe order by code.Some links:
rankbehaviour (to re-order codes) was added in Rank categorical #15422Original:
The docstrings for these two methods say that values_for_foo should preserve order, but for non-ordered Categoricals, the current implementation using self.codes is not order-preserving. Is this a problem cc @jorisvandenbossche ?
Categorical.values_for_rankexists pretty much to solve this problem for rank and value_counts. We have a special case in core.algorithms using values_for_rank that we should ideally avoid.