Skip to content

API: should unordered categoricals sort by codes or by categories? (sort vs rank inconsistency) #33245

Description

@jbrockmendel

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions