Merge pull request #48 from Magnus167/range-index-update

Allow explicit setting range index
This commit is contained in:
Palash Tyagi 2025-06-22 05:21:45 +01:00 committed by GitHub
commit 6e0ea441e4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

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)
@ -1117,10 +1124,10 @@ mod tests {
Frame::new(matrix, vec!["X", "Y"], Some(index)); Frame::new(matrix, vec!["X", "Y"], Some(index));
} }
#[test] #[test]
#[should_panic(expected = "Cannot explicitly provide a Range index")] #[should_panic(expected = "Frame::new: Range index length (4) mismatch matrix rows (3)")]
fn frame_new_panic_explicit_range() { fn frame_new_panic_invalid_explicit_range_index() {
let matrix = create_test_matrix_f64(); let matrix = create_test_matrix_f64(); // 3 rows
let index = RowIndex::Range(0..3); // User cannot provide Range directly let index = RowIndex::Range(0..4); // Range 0..4 but only 3 rows
Frame::new(matrix, vec!["A", "B"], Some(index)); Frame::new(matrix, vec!["A", "B"], Some(index));
} }