Skip to content

add DataFrame.sorted_indices #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Apr 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion spec/API_specification/dataframe_api/dataframe_object.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations
from typing import Sequence, Union, TYPE_CHECKING, NoReturn, Mapping

from typing import Literal, Mapping, Sequence, Union, TYPE_CHECKING, NoReturn


if TYPE_CHECKING:
from .column_object import Column
Expand Down Expand Up @@ -215,6 +217,47 @@ def get_column_names(self) -> Sequence[str]:
"""
...

def sorted_indices(
self,
keys: Sequence[str],
*,
ascending: Sequence[bool] | bool = True,
nulls_position: Literal['first', 'last'] = 'last',
) -> Column[int]:
"""
Return row numbers which would sort according to given columns.

If you need to sort the DataFrame, you can simply do::

df.get_rows(df.sorted_indices(keys))

Parameters
----------
keys : Sequence[str]
Names of columns to sort by.
ascending : Sequence[bool] or bool
If `True`, sort by all keys in ascending order.
If `False`, sort by all keys in descending order.
If a sequence, it must be the same length as `keys`,
and determines the direction with which to use each
key to sort by.
nulls_position : {'first', 'last'}
Whether null values should be placed at the beginning
or at the end of the result.
Note that the position of NaNs is unspecified and may
vary based on the implementation.

Returns
-------
Column[int]

Raises
------
ValueError
If `keys` and `ascending` are sequences of different lengths.
"""
...

def __eq__(self, other: DataFrame | Scalar) -> DataFrame:
"""
Compare for equality.
Expand Down