Skip to content

Commit

Permalink
fix[rust]: argsort argument expansion fixed (#4434)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Aug 16, 2022
1 parent 18faa47 commit 6f78893
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
6 changes: 1 addition & 5 deletions polars/polars-lazy/src/dsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,13 +501,9 @@ impl Expr {

/// Get the index values that would sort this expression.
pub fn arg_sort(self, reverse: bool) -> Self {
assert!(
!has_expr(&self, |e| matches!(e, Expr::Wildcard)),
"wildcard not supported in argsort expr"
);
let options = FunctionOptions {
collect_groups: ApplyOptions::ApplyGroups,
input_wildcard_expansion: true,
input_wildcard_expansion: false,
auto_explode: false,
fmt_str: "arg_sort",
};
Expand Down
19 changes: 19 additions & 0 deletions py-polars/tests/test_expr_multi_cols.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,22 @@ def test_expanding_sum() -> None:
assert df.with_column(pl.sum(pl.col(r"^y_.*$")).alias("y_sum"))[
"y_sum"
].to_list() == [2.1, 4.7, 6.8]


def test_argsort_argument_expansion() -> None:
df = pl.DataFrame(
{
"col1": [1, 2, 3],
"col2": [4, 5, 6],
"sort_order": [9, 8, 7],
}
)
assert df.select(
pl.col("col1").sort_by(pl.col("sort_order").arg_sort()).suffix("_suffix")
).to_dict(False) == {"col1_suffix": [3, 2, 1]}
assert df.select(
pl.col("^col.*$").sort_by(pl.col("sort_order")).arg_sort()
).to_dict(False) == {"col1": [2, 1, 0], "col2": [2, 1, 0]}
assert df.select(
pl.all().exclude("sort_order").sort_by(pl.col("sort_order")).arg_sort()
).to_dict(False) == {"col1": [2, 1, 0], "col2": [2, 1, 0]}

0 comments on commit 6f78893

Please sign in to comment.