Skip to content

Commit

Permalink
fix[rust]: fix unique order on 'last' keep setting (#4405)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Aug 14, 2022
1 parent d63c884 commit 98e46ba
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
16 changes: 14 additions & 2 deletions polars/polars-core/src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use crate::POOL;
use serde::{Deserialize, Serialize};
use std::hash::{BuildHasher, Hash, Hasher};

use crate::frame::groupby::GroupsIndicator;
use crate::series::IsSorted;
pub use chunks::*;

Expand Down Expand Up @@ -2968,9 +2969,20 @@ impl DataFrame {
self.apply_columns_par(&|s| unsafe { s.agg_first(groups) })
}
(Last, true) => {
let gb = self.groupby_stable(names)?;
// maintain order by last values, so the sorted groups are not correct as they
// are sorted by the first value
let gb = self.groupby(names)?;
let groups = gb.get_groups();
self.apply_columns_par(&|s| unsafe { s.agg_last(groups) })
let last_idx: NoNull<IdxCa> = groups
.iter()
.map(|g| match g {
GroupsIndicator::Idx((_first, idx)) => idx[idx.len() - 1],
GroupsIndicator::Slice([first, len]) => first + len,
})
.collect();

let last_idx = last_idx.sort(false);
return Ok(unsafe { self.take_unchecked(&last_idx) });
}
(First, false) => {
let gb = self.groupby(names)?;
Expand Down
12 changes: 12 additions & 0 deletions py-polars/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,15 @@ def test_argsort_sort_by_groups_update__4360() -> None:
]
)
)["1_argsort_2"].to_list() == [1, 2, 3, 300, 200, 100]


def test_unique_order() -> None:
df = pl.DataFrame({"a": [1, 2, 1]}).with_row_count()
assert df.unique(keep="last", subset="a", maintain_order=True).to_dict(False) == {
"row_nr": [1, 2],
"a": [2, 1],
}
assert df.unique(keep="first", subset="a", maintain_order=True).to_dict(False) == {
"row_nr": [0, 1],
"a": [1, 2],
}

0 comments on commit 98e46ba

Please sign in to comment.