Skip to content

Commit c1b1ab1

Browse files
authored
add DataFrame.sorted_indices (#128)
* add sort * add ascending arg, and nulls position * ascending: sequence[bool] or bool * leave nan position unspecified * sort => sorted_indices * fixup return type * add missing keys * update return type to column rather than sequence
1 parent f690016 commit c1b1ab1

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

spec/API_specification/dataframe_api/dataframe_object.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
2-
from typing import Sequence, Union, TYPE_CHECKING, NoReturn, Mapping
2+
3+
from typing import Literal, Mapping, Sequence, Union, TYPE_CHECKING, NoReturn
4+
35

46
if TYPE_CHECKING:
57
from .column_object import Column
@@ -239,6 +241,47 @@ def get_column_names(self) -> Sequence[str]:
239241
"""
240242
...
241243

244+
def sorted_indices(
245+
self,
246+
keys: Sequence[str],
247+
*,
248+
ascending: Sequence[bool] | bool = True,
249+
nulls_position: Literal['first', 'last'] = 'last',
250+
) -> Column[int]:
251+
"""
252+
Return row numbers which would sort according to given columns.
253+
254+
If you need to sort the DataFrame, you can simply do::
255+
256+
df.get_rows(df.sorted_indices(keys))
257+
258+
Parameters
259+
----------
260+
keys : Sequence[str]
261+
Names of columns to sort by.
262+
ascending : Sequence[bool] or bool
263+
If `True`, sort by all keys in ascending order.
264+
If `False`, sort by all keys in descending order.
265+
If a sequence, it must be the same length as `keys`,
266+
and determines the direction with which to use each
267+
key to sort by.
268+
nulls_position : {'first', 'last'}
269+
Whether null values should be placed at the beginning
270+
or at the end of the result.
271+
Note that the position of NaNs is unspecified and may
272+
vary based on the implementation.
273+
274+
Returns
275+
-------
276+
Column[int]
277+
278+
Raises
279+
------
280+
ValueError
281+
If `keys` and `ascending` are sequences of different lengths.
282+
"""
283+
...
284+
242285
def __eq__(self, other: DataFrame | Scalar) -> DataFrame:
243286
"""
244287
Compare for equality.

0 commit comments

Comments
 (0)