-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(bigframes): Support unstable sort_values, sort_index #16665
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2418,6 +2418,7 @@ def sort_index( | |||||||||
| *, | ||||||||||
| ascending: bool = ..., | ||||||||||
| inplace: Literal[False] = ..., | ||||||||||
| kind: str = ..., | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||
| na_position: Literal["first", "last"] = ..., | ||||||||||
| ) -> DataFrame: ... | ||||||||||
|
|
||||||||||
|
|
@@ -2427,6 +2428,7 @@ def sort_index( | |||||||||
| *, | ||||||||||
| ascending: bool = ..., | ||||||||||
| inplace: Literal[True] = ..., | ||||||||||
| kind: str = ..., | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||
| na_position: Literal["first", "last"] = ..., | ||||||||||
| ) -> None: ... | ||||||||||
|
|
||||||||||
|
|
@@ -2436,6 +2438,7 @@ def sort_index( | |||||||||
| axis: Union[int, str] = 0, | ||||||||||
| ascending: bool = True, | ||||||||||
| inplace: bool = False, | ||||||||||
| kind: str | None = None, | ||||||||||
| na_position: Literal["first", "last"] = "last", | ||||||||||
| ) -> Optional[DataFrame]: | ||||||||||
| if utils.get_axis_number(axis) == 0: | ||||||||||
|
|
@@ -2449,7 +2452,8 @@ def sort_index( | |||||||||
| else order.descending_over(column, na_last) | ||||||||||
| for column in index_columns | ||||||||||
| ] | ||||||||||
| block = self._block.order_by(ordering) | ||||||||||
| is_stable = (kind or constants.DEFAULT_SORT_KIND) in ["stable", "mergesort"] | ||||||||||
| block = self._block.order_by(ordering, stable=is_stable) | ||||||||||
|
Comment on lines
+2455
to
+2456
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||||||
| else: # axis=1 | ||||||||||
| _, indexer = self.columns.sort_values( | ||||||||||
| return_indexer=True, | ||||||||||
|
|
@@ -2472,7 +2476,7 @@ def sort_values( | |||||||||
| *, | ||||||||||
| inplace: Literal[False] = ..., | ||||||||||
| ascending: bool | typing.Sequence[bool] = ..., | ||||||||||
| kind: str = ..., | ||||||||||
| kind: str | None = None, | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||
| na_position: typing.Literal["first", "last"] = ..., | ||||||||||
| ) -> DataFrame: ... | ||||||||||
|
|
||||||||||
|
|
@@ -2483,7 +2487,7 @@ def sort_values( | |||||||||
| *, | ||||||||||
| inplace: Literal[True] = ..., | ||||||||||
| ascending: bool | typing.Sequence[bool] = ..., | ||||||||||
| kind: str = ..., | ||||||||||
| kind: str | None = None, | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||
| na_position: typing.Literal["first", "last"] = ..., | ||||||||||
| ) -> None: ... | ||||||||||
|
|
||||||||||
|
|
@@ -2493,7 +2497,7 @@ def sort_values( | |||||||||
| *, | ||||||||||
| inplace: bool = False, | ||||||||||
| ascending: bool | typing.Sequence[bool] = True, | ||||||||||
| kind: str = "quicksort", | ||||||||||
| kind: str | None = None, | ||||||||||
| na_position: typing.Literal["first", "last"] = "last", | ||||||||||
| ) -> Optional[DataFrame]: | ||||||||||
| if isinstance(by, (bigframes.series.Series, indexes.Index, DataFrame)): | ||||||||||
|
|
@@ -2525,7 +2529,8 @@ def sort_values( | |||||||||
| if is_ascending | ||||||||||
| else order.descending_over(column_id, na_last) | ||||||||||
| ) | ||||||||||
| block = self._block.order_by(ordering) | ||||||||||
| is_stable = (kind or constants.DEFAULT_SORT_KIND) in ["stable", "mergesort"] | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||
| block = self._block.order_by(ordering, stable=is_stable) | ||||||||||
| if inplace: | ||||||||||
| self._set_block(block) | ||||||||||
| return None | ||||||||||
|
|
||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1769,7 +1769,7 @@ def sort_values( | |
| axis=..., | ||
| inplace: Literal[True] = ..., | ||
| ascending: bool | typing.Sequence[bool] = ..., | ||
| kind: str = ..., | ||
| kind: str | None = ..., | ||
| na_position: typing.Literal["first", "last"] = ..., | ||
| ) -> None: ... | ||
|
|
||
|
|
@@ -1780,7 +1780,7 @@ def sort_values( | |
| axis=..., | ||
| inplace: Literal[False] = ..., | ||
| ascending: bool | typing.Sequence[bool] = ..., | ||
| kind: str = ..., | ||
| kind: str | None = ..., | ||
| na_position: typing.Literal["first", "last"] = ..., | ||
| ) -> Series: ... | ||
|
|
||
|
|
@@ -1790,19 +1790,21 @@ def sort_values( | |
| axis=0, | ||
| inplace: bool = False, | ||
| ascending=True, | ||
| kind: str = "quicksort", | ||
| kind: str | None = None, | ||
| na_position: typing.Literal["first", "last"] = "last", | ||
| ) -> Optional[Series]: | ||
| if axis != 0 and axis != "index": | ||
| raise ValueError(f"No axis named {axis} for object type Series") | ||
| if na_position not in ["first", "last"]: | ||
| raise ValueError("Param na_position must be one of 'first' or 'last'") | ||
| is_stable = (kind or constants.DEFAULT_SORT_KIND) in ["stable", "mergesort"] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| block = self._block.order_by( | ||
| [ | ||
| order.ascending_over(self._value_column, (na_position == "last")) | ||
| if ascending | ||
| else order.descending_over(self._value_column, (na_position == "last")) | ||
| ], | ||
| stable=is_stable, | ||
| ) | ||
| if inplace: | ||
| self._set_block(block) | ||
|
|
@@ -1812,17 +1814,37 @@ def sort_values( | |
|
|
||
| @typing.overload # type: ignore[override] | ||
| def sort_index( | ||
| self, *, axis=..., inplace: Literal[False] = ..., ascending=..., na_position=... | ||
| ) -> Series: ... | ||
| self, | ||
| *, | ||
| axis=..., | ||
| inplace: Literal[False] = ..., | ||
| ascending=..., | ||
| kind: str | None = ..., | ||
| na_position=..., | ||
| ) -> Series: | ||
| ... | ||
|
|
||
| @typing.overload | ||
| def sort_index( | ||
| self, *, axis=0, inplace: Literal[True] = ..., ascending=..., na_position=... | ||
| ) -> None: ... | ||
| self, | ||
| *, | ||
| axis=0, | ||
| inplace: Literal[True] = ..., | ||
| ascending=..., | ||
| kind: str | None = ..., | ||
| na_position=..., | ||
| ) -> None: | ||
| ... | ||
|
|
||
| @validations.requires_index | ||
| def sort_index( | ||
| self, *, axis=0, inplace: bool = False, ascending=True, na_position="last" | ||
| self, | ||
| *, | ||
| axis=0, | ||
| inplace: bool = False, | ||
| ascending=True, | ||
| kind: str | None = None, | ||
| na_position="last", | ||
| ) -> Optional[Series]: | ||
| # TODO(tbergeron): Support level parameter once multi-index introduced. | ||
| if axis != 0 and axis != "index": | ||
|
|
@@ -1837,7 +1859,8 @@ def sort_index( | |
| else order.descending_over(column, na_last) | ||
| for column in block.index_columns | ||
| ] | ||
| block = block.order_by(ordering) | ||
| is_stable = (kind or constants.DEFAULT_SORT_KIND) in ["stable", "mergesort"] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| block = block.order_by(ordering, stable=is_stable) | ||
| if inplace: | ||
| self._set_block(block) | ||
| return None | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,3 +55,5 @@ | |
| "_deferred", | ||
| ] | ||
| VALID_WRITE_ENGINES = typing.get_args(WriteEngineType) | ||
|
|
||
| DEFAULT_SORT_KIND = "stable" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use the
STABLE_SORT_KINDSconstant for consistency and easier maintenance.