diff --git a/crates/polars-core/src/frame/group_by/proxy.rs b/crates/polars-core/src/frame/group_by/proxy.rs index 70a478f2da53..d8bc3b4c60e3 100644 --- a/crates/polars-core/src/frame/group_by/proxy.rs +++ b/crates/polars-core/src/frame/group_by/proxy.rs @@ -367,19 +367,16 @@ impl GroupsProxy { } } - pub fn take_group_lasts(self) -> Vec { + /// # Safety + /// This will not do any bounds checks. The caller must ensure + /// all groups have members. + pub unsafe fn take_group_lasts(self) -> Vec { match self { - GroupsProxy::Idx(groups) => { - groups - .all - .iter() - .map(|idx| { - // safety: - // idx has at least one eletment, so -1 is always in bounds - unsafe { *idx.get_unchecked(idx.len() - 1) } - }) - .collect() - }, + GroupsProxy::Idx(groups) => groups + .all + .iter() + .map(|idx| *idx.get_unchecked(idx.len() - 1)) + .collect(), GroupsProxy::Slice { groups, .. } => groups .into_iter() .map(|[first, len]| first + len - 1) diff --git a/crates/polars-ops/src/series/ops/is_last_distinct.rs b/crates/polars-ops/src/series/ops/is_last_distinct.rs index 9cb00799dacf..57c388f2c5fc 100644 --- a/crates/polars-ops/src/series/ops/is_last_distinct.rs +++ b/crates/polars-ops/src/series/ops/is_last_distinct.rs @@ -149,7 +149,8 @@ where #[cfg(feature = "dtype-struct")] fn is_last_distinct_struct(s: &Series) -> PolarsResult { let groups = s.group_tuples(true, false)?; - let last = groups.take_group_lasts(); + // SAFETY: all groups have at least a single member + let last = unsafe { groups.take_group_lasts() }; let mut out = MutableBitmap::with_capacity(s.len()); out.extend_constant(s.len(), false); @@ -165,7 +166,8 @@ fn is_last_distinct_struct(s: &Series) -> PolarsResult { #[cfg(feature = "group_by_list")] fn is_last_distinct_list(ca: &ListChunked) -> PolarsResult { let groups = ca.group_tuples(true, false)?; - let last = groups.take_group_lasts(); + // SAFETY: all groups have at least a single member + let last = unsafe { groups.take_group_lasts() }; let mut out = MutableBitmap::with_capacity(ca.len()); out.extend_constant(ca.len(), false);