Validate range index length in Frame::new to prevent mismatches with matrix rows

This commit is contained in:
Palash Tyagi 2025-06-22 05:08:05 +01:00
parent 349ae52629
commit 092a7b7cce

View File

@ -232,11 +232,18 @@ impl<T: Clone + PartialEq> Frame<T> {
} }
(RowIndex::Date(vals), RowIndexLookup::Date(lookup)) (RowIndex::Date(vals), RowIndexLookup::Date(lookup))
} }
Some(RowIndex::Range(_)) => { Some(RowIndex::Range(ref r)) => {
// If the length of the range does not match the number of rows, panic.
if r.end.saturating_sub(r.start) != num_rows {
panic!( panic!(
"Frame::new: Cannot explicitly provide a Range index. Use None for default range." "Frame::new: Range index length ({}) mismatch matrix rows ({})",
r.end.saturating_sub(r.start),
num_rows
); );
} }
// return the range as is.
(RowIndex::Range(r.clone()), RowIndexLookup::None)
}
None => { None => {
// Default to a sequential range index. // Default to a sequential range index.
(RowIndex::Range(0..num_rows), RowIndexLookup::None) (RowIndex::Range(0..num_rows), RowIndexLookup::None)