Skip to content

Commit 77bc66b

Browse files
authored
Add DataFrame.sort and Column.sort (#234)
* add sort * update sorted_indices docs
1 parent cafa8fd commit 77bc66b

File tree

2 files changed

+72
-6
lines changed

2 files changed

+72
-6
lines changed

spec/API_specification/dataframe_api/column_object.py

+30-3
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,35 @@ def get_value(self, row_number: int) -> Scalar:
141141
"""
142142
...
143143

144+
def sort(
145+
self,
146+
*,
147+
ascending: bool = True,
148+
nulls_position: Literal['first', 'last'] = 'last',
149+
) -> Column[DType]:
150+
"""
151+
Sort column.
152+
153+
If you need the indices which would sort the column,
154+
use :meth:`sorted_indices`.
155+
156+
Parameters
157+
----------
158+
ascending : bool
159+
If `True`, sort in ascending order.
160+
If `False`, sort in descending order.
161+
nulls_position : ``{'first', 'last'}``
162+
Whether null values should be placed at the beginning
163+
or at the end of the result.
164+
Note that the position of NaNs is unspecified and may
165+
vary based on the implementation.
166+
167+
Returns
168+
-------
169+
Column
170+
"""
171+
...
172+
144173
def sorted_indices(
145174
self,
146175
*,
@@ -150,9 +179,7 @@ def sorted_indices(
150179
"""
151180
Return row numbers which would sort column.
152181
153-
If you need to sort the Column, you can simply do::
154-
155-
col.get_rows(col.sorted_indices())
182+
If you need to sort the Column, use :meth:`sort`.
156183
157184
Parameters
158185
----------

spec/API_specification/dataframe_api/dataframe_object.py

+42-3
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,47 @@ def get_column_names(self) -> Sequence[str]:
244244
Sequence[str]
245245
"""
246246
...
247+
248+
def sort(
249+
self,
250+
keys: Sequence[str] | None = None,
251+
*,
252+
ascending: Sequence[bool] | bool = True,
253+
nulls_position: Literal['first', 'last'] = 'last',
254+
) -> DataFrame:
255+
"""
256+
Sort dataframe according to given columns.
257+
258+
If you only need the indices which would sort the dataframe, use
259+
:meth:`sorted_indices`.
260+
261+
Parameters
262+
----------
263+
keys : Sequence[str] | None
264+
Names of columns to sort by.
265+
If `None`, sort by all columns.
266+
ascending : Sequence[bool] or bool
267+
If `True`, sort by all keys in ascending order.
268+
If `False`, sort by all keys in descending order.
269+
If a sequence, it must be the same length as `keys`,
270+
and determines the direction with which to use each
271+
key to sort by.
272+
nulls_position : ``{'first', 'last'}``
273+
Whether null values should be placed at the beginning
274+
or at the end of the result.
275+
Note that the position of NaNs is unspecified and may
276+
vary based on the implementation.
277+
278+
Returns
279+
-------
280+
DataFrame
281+
282+
Raises
283+
------
284+
ValueError
285+
If `keys` and `ascending` are sequences of different lengths.
286+
"""
287+
...
247288

248289
def sorted_indices(
249290
self,
@@ -255,9 +296,7 @@ def sorted_indices(
255296
"""
256297
Return row numbers which would sort according to given columns.
257298
258-
If you need to sort the DataFrame, you can simply do::
259-
260-
df.get_rows(df.sorted_indices(keys))
299+
If you need to sort the DataFrame, use :meth:`sort`.
261300
262301
Parameters
263302
----------

0 commit comments

Comments
 (0)