Skip to content

ENH: In sort methods, accept sequence or dict as key #57216

Closed
@gsehrlich

Description

@gsehrlich

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

Pretty much every time I use the key kwarg of a pandas sort method, I use it to tell pandas the desired order of the unique values. For example:

import pandas as pd

my_index = pd.Index(['one', 'two', 'three', 'three', 'two', 'one', 'three'])
my_index.sort_values(
    key=lambda idx: (idx
        .to_series()
        .rename(index={'one': 1, 'two': 2, 'three': 3})
        .index
    )
)

This is all a bit redundant, and I suspect it's a common use case.

Relatedly, sometimes I want to use a different key function for different columns/levels. In order to do that, I actually need to call the sort method multiple times.

Feature Description

Accept sequences like ['one', 'two', 'three'] specifying the ascending order of the values.

EDIT: Example equivalent to the sort_values call above:

my_index.sort_values(key=['one', 'two', 'three'])

Also accept dictionaries like {'col_1': ['one', 'two', 'three'], 'col_3': my_sort_fn} to sort different columns or levels with different keys in any format.

Alternative Solutions

Make sure all data that I want to lexsort is always an ordered Categorical.

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementIndexRelated to the Index class or subclassesNeeds TriageIssue that has not been reviewed by a pandas team memberSortinge.g. sort_index, sort_values

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions