Skip to content

Commit

Permalink
feat(python)!: Only accept a single column in set_sorted
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Jun 7, 2024
1 parent 2e633dc commit a157f82
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
22 changes: 14 additions & 8 deletions py-polars/polars/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -10305,26 +10305,32 @@ def merge_sorted(self, other: DataFrame, key: str) -> DataFrame:

def set_sorted(
self,
column: str | Iterable[str],
*more_columns: str,
column: str,
*,
descending: bool = False,
) -> DataFrame:
"""
Indicate that one or multiple columns are sorted.
This can speed up future operations.
Parameters
----------
column
Columns that are sorted
more_columns
Additional columns that are sorted, specified as positional arguments.
Column that are sorted
descending
Whether the columns are sorted in descending order.
Warnings
--------
This can lead to incorrect results if the data is NOT sorted!!
Use with care!
"""
# NOTE: Only accepts 1 column on purpose! User think they are sorted by
# the combined multicolumn values.
return (
self.lazy()
.set_sorted(column, *more_columns, descending=descending)
.collect(_eager=True)
self.lazy().set_sorted(column, descending=descending).collect(_eager=True)
)

@unstable()
Expand Down
2 changes: 1 addition & 1 deletion py-polars/polars/expr/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -9811,7 +9811,7 @@ def set_sorted(self, *, descending: bool = False) -> Self:
Warnings
--------
This can lead to incorrect results if this `Series` is not sorted!!
This can lead to incorrect results if the data is NOT sorted!!
Use with care!
Examples
Expand Down
27 changes: 17 additions & 10 deletions py-polars/polars/lazyframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
from polars.slice import LazyPolarsSlice

with contextlib.suppress(ImportError): # Module not available when building docs
from polars.polars import PyLazyFrame
from polars.polars import PyLazyFrame, col

if TYPE_CHECKING:
import sys
Expand Down Expand Up @@ -5895,27 +5895,34 @@ def merge_sorted(self, other: LazyFrame, key: str) -> Self:

def set_sorted(
self,
column: str | Iterable[str],
*more_columns: str,
column: str,
*,
descending: bool = False,
) -> Self:
"""
Indicate that one or multiple columns are sorted.
This can speed up future operations.
Parameters
----------
column
Columns that are sorted
more_columns
Additional columns that are sorted, specified as positional arguments.
descending
Whether the columns are sorted in descending order.
"""
columns = parse_as_list_of_expressions(column, *more_columns)
return self.with_columns(
[wrap_expr(e).set_sorted(descending=descending) for e in columns]
)
Warnings
--------
This can lead to incorrect results if the data is NOT sorted!!
Use with care!
"""
# NOTE: Only accepts 1 column on purpose! User think they are sorted by
# the combined multicolumn values.
if not isinstance(column, str):
msg = "expected a 'str' for argument 'column' in 'set_sorted'"
raise TypeError(msg)
return self.with_columns(col(column).set_sorted(descending=descending))

@unstable()
def update(
Expand Down
7 changes: 7 additions & 0 deletions py-polars/tests/unit/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,3 +658,10 @@ def test_raise_invalid_arithmetic() -> None:

with pytest.raises(pl.InvalidOperationError):
df.select(pl.col("a") - pl.col("a"))


def test_raise_on_sorted_multi_args() -> None:
with pytest.raises(TypeError):
pl.DataFrame({"a": [1], "b": [1]}).set_sorted(
["a", "b"] # type: ignore[arg-type]
)

0 comments on commit a157f82

Please sign in to comment.