Skip to content

Commit

Permalink
fix(rust, python): include slice in sort fast path (#5247)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Oct 18, 2022
1 parent 48376b5 commit c87679f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
7 changes: 6 additions & 1 deletion polars/polars-core/src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1786,7 +1786,12 @@ impl DataFrame {
// no need to compute the sort indices and then take by these indices
// simply sort and return as frame
if self.width() == 1 && self.check_name_to_idx(s.name()).is_ok() {
return Ok(s.sort_with(options).into_frame());
let mut out = s.sort_with(options);
if let Some((offset, len)) = slice {
out = out.slice(offset, len);
}

return Ok(out.into_frame());
}
s.argsort(options)
}
Expand Down
13 changes: 13 additions & 0 deletions py-polars/tests/unit/test_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,16 @@ def test_unset_sorted_flag_after_extend() -> None:
df = df1.groupby("Add").agg([pl.col("Batch").min()]).sort("Add")
assert df["Add"].flags["SORTED_ASC"]
assert df.to_dict(False) == {"Add": [37, 41], "Batch": [48, 49]}


def test_sort_slice_fast_path_5245() -> None:
df = pl.DataFrame(
{
"foo": ["f", "c", "b", "a"],
"bar": [1, 2, 3, 4],
}
).lazy()

assert df.sort("foo").limit(1).select("foo").collect().to_dict(False) == {
"foo": ["a"]
}

0 comments on commit c87679f

Please sign in to comment.