Sort the dataset by a single column. Sorting a parallel dataset requires expensive shuffles and is generally not recommended. See ``set_index`` for implementation details. Parameters ---------- by: str or list[str] Column(s) to sort by. n
(
self,
by: str | list[str],
npartitions: int | None = None,
ascending: bool | list[bool] = True,
na_position: Literal["first", "last"] = "last",
partition_size: float = 128e6,
sort_function: Callable[[pd.DataFrame], pd.DataFrame] | None = None,
sort_function_kwargs: Mapping[str, Any] | None = None,
upsample: float = 1.0,
ignore_index: bool | None = False,
shuffle_method: str | None = None,
**options,
)
| 3562 | ) |
| 3563 | |
| 3564 | def sort_values( |
| 3565 | self, |
| 3566 | by: str | list[str], |
| 3567 | npartitions: int | None = None, |
| 3568 | ascending: bool | list[bool] = True, |
| 3569 | na_position: Literal["first", "last"] = "last", |
| 3570 | partition_size: float = 128e6, |
| 3571 | sort_function: Callable[[pd.DataFrame], pd.DataFrame] | None = None, |
| 3572 | sort_function_kwargs: Mapping[str, Any] | None = None, |
| 3573 | upsample: float = 1.0, |
| 3574 | ignore_index: bool | None = False, |
| 3575 | shuffle_method: str | None = None, |
| 3576 | **options, |
| 3577 | ): |
| 3578 | """Sort the dataset by a single column. |
| 3579 | |
| 3580 | Sorting a parallel dataset requires expensive shuffles and is generally |
| 3581 | not recommended. See ``set_index`` for implementation details. |
| 3582 | |
| 3583 | Parameters |
| 3584 | ---------- |
| 3585 | by: str or list[str] |
| 3586 | Column(s) to sort by. |
| 3587 | npartitions: int, None, or 'auto' |
| 3588 | The ideal number of output partitions. If None, use the same as |
| 3589 | the input. If 'auto' then decide by memory use. |
| 3590 | ascending: bool, optional |
| 3591 | Sort ascending vs. descending. |
| 3592 | Defaults to True. |
| 3593 | na_position: {'last', 'first'}, optional |
| 3594 | Puts NaNs at the beginning if 'first', puts NaN at the end if 'last'. |
| 3595 | Defaults to 'last'. |
| 3596 | sort_function: function, optional |
| 3597 | Sorting function to use when sorting underlying partitions. |
| 3598 | If None, defaults to ``M.sort_values`` (the partition library's |
| 3599 | implementation of ``sort_values``). |
| 3600 | sort_function_kwargs: dict, optional |
| 3601 | Additional keyword arguments to pass to the partition sorting function. |
| 3602 | By default, ``by``, ``ascending``, and ``na_position`` are provided. |
| 3603 | |
| 3604 | Examples |
| 3605 | -------- |
| 3606 | >>> df2 = df.sort_values('x') # doctest: +SKIP |
| 3607 | """ |
| 3608 | if na_position not in ("first", "last"): |
| 3609 | raise ValueError("na_position must be either 'first' or 'last'") |
| 3610 | if not isinstance(by, list): |
| 3611 | by = [by] |
| 3612 | if any(not isinstance(b, str) for b in by): |
| 3613 | raise NotImplementedError( |
| 3614 | "Dataframes only support sorting by named columns which must be passed as a " |
| 3615 | "string or a list of strings.\n" |
| 3616 | f"You passed {by}" |
| 3617 | ) |
| 3618 | |
| 3619 | if not isinstance(ascending, bool) and not len(ascending) == len(by): |
| 3620 | raise ValueError(f"Length of {ascending=} != length of {by=}") |
| 3621 |