Skip to content

Add DataFrame.sort and Column.sort #234

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 2 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 29 additions & 0 deletions spec/API_specification/dataframe_api/column_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,35 @@ def get_value(self, row_number: int) -> Scalar:
"""
...

def sort(
self,
*,
ascending: bool = True,
nulls_position: Literal['first', 'last'] = 'last',
) -> Column[DType]:
"""
Sort column.

If you need the indices which would sort the column,
use :meth:`sorted_indices`.

Parameters
----------
ascending : bool
If `True`, sort in ascending order.
If `False`, sort in descending order.
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
"""
...

def sorted_indices(
self,
*,
Expand Down
41 changes: 41 additions & 0 deletions spec/API_specification/dataframe_api/dataframe_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,47 @@ def get_column_names(self) -> Sequence[str]:
Sequence[str]
"""
...

def sort(
self,
keys: Sequence[str] | None = None,
*,
ascending: Sequence[bool] | bool = True,
nulls_position: Literal['first', 'last'] = 'last',
) -> DataFrame:
"""
Sort dataframe according to given columns.

If you only need the indices which would sort the dataframe, use
:meth:`sorted_indices`.

Parameters
----------
keys : Sequence[str] | None
Names of columns to sort by.
If `None`, sort by all columns.
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
-------
DataFrame

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

def sorted_indices(
self,
Expand Down