Skip to content

Commit

Permalink
fix(rust, python): allow empty sort on any dtype (#5975)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Jan 1, 2023
1 parent 96cfab4 commit ab317c7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions polars/polars-core/src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1837,6 +1837,9 @@ impl DataFrame {
nulls_last: bool,
slice: Option<(i64, usize)>,
) -> PolarsResult<Self> {
if self.height() == 0 {
return Ok(self.clone());
}
// note that the by_column argument also contains evaluated expression from polars-lazy
// that may not even be present in this dataframe.

Expand Down
15 changes: 15 additions & 0 deletions py-polars/tests/unit/test_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,18 @@ def test_object_in_struct() -> None:
assert (arr == np_a).sum() == 3
arr = out["foo"][1]["B"]
assert (arr == np_b).sum() == 3


def test_empty_sort() -> None:
df = pl.DataFrame(
data=[
({"name": "bar", "sort_key": 2},),
({"name": "foo", "sort_key": 1},),
],
columns=[
("blob", pl.Object),
],
orient="row",
)
df_filtered = df.filter(pl.col("blob").apply(lambda blob: blob["name"] == "baz"))
df_filtered.sort(pl.col("blob").apply(lambda blob: blob["sort_key"]))

0 comments on commit ab317c7

Please sign in to comment.