Skip to content

Commit 6e0ea44

Browse files
authored
Merge pull request #48 from Magnus167/range-index-update
Allow explicit setting range index
2 parents 349ae52 + 453e34e commit 6e0ea44

1 file changed

Lines changed: 15 additions & 8 deletions

File tree

src/frame/base.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,17 @@ impl<T: Clone + PartialEq> Frame<T> {
232232
}
233233
(RowIndex::Date(vals), RowIndexLookup::Date(lookup))
234234
}
235-
Some(RowIndex::Range(_)) => {
236-
panic!(
237-
"Frame::new: Cannot explicitly provide a Range index. Use None for default range."
238-
);
235+
Some(RowIndex::Range(ref r)) => {
236+
// If the length of the range does not match the number of rows, panic.
237+
if r.end.saturating_sub(r.start) != num_rows {
238+
panic!(
239+
"Frame::new: Range index length ({}) mismatch matrix rows ({})",
240+
r.end.saturating_sub(r.start),
241+
num_rows
242+
);
243+
}
244+
// return the range as is.
245+
(RowIndex::Range(r.clone()), RowIndexLookup::None)
239246
}
240247
None => {
241248
// Default to a sequential range index.
@@ -1117,10 +1124,10 @@ mod tests {
11171124
Frame::new(matrix, vec!["X", "Y"], Some(index));
11181125
}
11191126
#[test]
1120-
#[should_panic(expected = "Cannot explicitly provide a Range index")]
1121-
fn frame_new_panic_explicit_range() {
1122-
let matrix = create_test_matrix_f64();
1123-
let index = RowIndex::Range(0..3); // User cannot provide Range directly
1127+
#[should_panic(expected = "Frame::new: Range index length (4) mismatch matrix rows (3)")]
1128+
fn frame_new_panic_invalid_explicit_range_index() {
1129+
let matrix = create_test_matrix_f64(); // 3 rows
1130+
let index = RowIndex::Range(0..4); // Range 0..4 but only 3 rows
11241131
Frame::new(matrix, vec!["A", "B"], Some(index));
11251132
}
11261133

0 commit comments

Comments
 (0)