-
Notifications
You must be signed in to change notification settings - Fork 181
feat: add list.sort
#3356
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
feat: add list.sort
#3356
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
97af862
feat: add `list_sort`
raisadz ab3c88e
Merge remote-tracking branch 'upstream/main' into feat/list-sort
raisadz 7c2f1f3
add not_implemented to pyarrow and pandas
raisadz 42cfcd5
skip old polars as `nulls_last` arg was not implemented
raisadz 4402e98
skip old polars again
raisadz 5ce0cf9
update pyarrow issue link
raisadz f1308bb
implement pyarrow sort
raisadz 7b0bc82
Merge remote-tracking branch 'upstream/main' into feat/list-sort
raisadz ea59694
use arange from utils
raisadz 58b86ad
skip old pandas
raisadz aa16798
change var names
raisadz 5343e0f
fix typing
raisadz d0a06a2
Merge remote-tracking branch 'upstream/main' into feat/list-sort
raisadz 4107ad1
refactor pandas, remove tests fot the default params
raisadz 5f2834b
Merge remote-tracking branch 'upstream/main' into feat/list-sort
raisadz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
| - mean | ||
| - median | ||
| - min | ||
| - sort | ||
| - sum | ||
| - unique | ||
| show_source: false | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
| - mean | ||
| - median | ||
| - min | ||
| - sort | ||
| - sum | ||
| - unique | ||
| show_source: false | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from functools import partial | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from narwhals._compliant.any_namespace import ListNamespace | ||
|
|
@@ -34,35 +35,28 @@ def len(self) -> PandasLikeSeries: | |
| ) | ||
| return self.with_native(result.astype(dtype)).alias(self.native.name) | ||
|
|
||
| unique = not_implemented() | ||
|
|
||
| contains = not_implemented() | ||
|
|
||
| def get(self, index: int) -> PandasLikeSeries: | ||
| result = self.native.list[index] | ||
| result.name = self.native.name | ||
| return self.with_native(result) | ||
|
|
||
| def _agg( | ||
| self, func: Literal["min", "max", "mean", "approximate_median", "sum"] | ||
| ) -> PandasLikeSeries: | ||
| def _raise_if_not_pyarrow_backend(self) -> None: | ||
| dtype_backend = get_dtype_backend( | ||
| self.native.dtype, self.compliant._implementation | ||
| ) | ||
| if dtype_backend != "pyarrow": # pragma: no cover | ||
| msg = "Only pyarrow backend is currently supported." | ||
| raise NotImplementedError(msg) | ||
|
|
||
| from narwhals._arrow.utils import list_agg, native_to_narwhals_dtype | ||
| def _agg( | ||
| self, func: Literal["min", "max", "mean", "approximate_median", "sum"] | ||
| ) -> PandasLikeSeries: | ||
| self._raise_if_not_pyarrow_backend() | ||
|
|
||
| from narwhals._arrow.utils import list_agg | ||
|
|
||
| ca = self.native.array._pa_array | ||
| result_arr = list_agg(ca, func) | ||
| nw_dtype = native_to_narwhals_dtype(result_arr.type, self.version) | ||
| out_dtype = narwhals_to_native_dtype( | ||
| nw_dtype, "pyarrow", self.implementation, self.version | ||
| ) | ||
| result_native = type(self.native)( | ||
| result_arr, dtype=out_dtype, index=self.native.index, name=self.native.name | ||
| result_native = self.compliant._apply_pyarrow_compute_func( | ||
| self.native, partial(list_agg, func=func) | ||
| ) | ||
| return self.with_native(result_native) | ||
|
|
||
|
|
@@ -80,3 +74,16 @@ def median(self) -> PandasLikeSeries: | |
|
|
||
| def sum(self) -> PandasLikeSeries: | ||
| return self._agg("sum") | ||
|
|
||
| def sort(self, *, descending: bool, nulls_last: bool) -> PandasLikeSeries: | ||
| self._raise_if_not_pyarrow_backend() | ||
|
|
||
| from narwhals._arrow.utils import list_sort | ||
|
|
||
| result_native = self.compliant._apply_pyarrow_compute_func( | ||
| self.native, partial(list_sort, descending=descending, nulls_last=nulls_last) | ||
| ) | ||
|
Member
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. @FBruzzesi factored some repeated logic here out using |
||
| return self.with_native(result_native) | ||
|
|
||
| unique = not_implemented() | ||
| contains = not_implemented() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.