Skip to content
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

Fix DF.sort_by/3 with groups to respect :nils option #886

Merged
merged 2 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions native/explorer/src/dataframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,17 @@ pub fn df_sort_by(
multithreaded,
)?
} else {
df.group_by_stable(groups)?
.apply(|df| df.sort(by_columns.clone(), reverse.clone(), maintain_order))?
df.group_by_stable(groups)?.apply(|df| {
let by_columns = df.select_series(&by_columns)?;
df.sort_impl(
by_columns,
reverse.clone(),
nulls_last,
maintain_order,
None,
multithreaded,
)
})?
};

Ok(ExDataFrame::new(new_df))
Expand Down
15 changes: 15 additions & 0 deletions test/explorer/data_frame/grouped_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,21 @@ defmodule Explorer.DataFrame.GroupedTest do
|> DF.pull("total")
|> Series.first() == 2175
end

test "sorts by group keeping nils last" do
with_nil =
DF.new(%{
id: [1, 1, 1, 2, 2, 2, 3, 3, 3],
data: [1, 0.5, nil, 0.7, 1, 0.9, 0.2, 0.2, 0.3]
})

grouped = DF.group_by(with_nil, :id)

assert grouped |> DF.sort_by(data, nils: :last) |> DF.to_columns(atom_keys: true) == %{
data: [0.5, 1.0, nil, 0.7, 0.9, 1.0, 0.2, 0.2, 0.3],
id: [1, 1, 1, 2, 2, 2, 3, 3, 3]
}
end
end

describe "sort_with/2" do
Expand Down
Loading