pub struct Frame<T: Clone + PartialEq> { /* private fields */ }
Implementations§
Source§impl<T: Clone + PartialEq> Frame<T>
impl<T: Clone + PartialEq> Frame<T>
Sourcepub fn new<L: Into<String>>(
matrix: Matrix<T>,
names: Vec<L>,
index: Option<RowIndex>,
) -> Self
pub fn new<L: Into<String>>( matrix: Matrix<T>, names: Vec<L>, index: Option<RowIndex>, ) -> Self
Creates a new Frame
from a matrix, column names, and an optional row index.
Panics if the underlying Matrix
requirements are not met (e.g., rows>0, cols>0 for standard constructors),
or if column name/index constraints are violated.
Sourcepub fn matrix_mut(&mut self) -> &mut Matrix<T>
pub fn matrix_mut(&mut self) -> &mut Matrix<T>
Returns a mutable reference to the underlying Matrix
.
Use with caution: direct matrix edits bypass frame-level name/index consistency checks.
Sourcepub fn column_index(&self, name: &str) -> Option<usize>
pub fn column_index(&self, name: &str) -> Option<usize>
Returns the physical column index for the given column name, if present.
Sourcepub fn column(&self, name: &str) -> &[T]
pub fn column(&self, name: &str) -> &[T]
Returns an immutable slice of the specified column’s data. Panics if the column name is not found.
Sourcepub fn column_mut(&mut self, name: &str) -> &mut [T]
pub fn column_mut(&mut self, name: &str) -> &mut [T]
Returns a mutable slice of the specified column’s data. Panics if the column name is not found.
Sourcepub fn get_row(&self, index_key: usize) -> FrameRowView<'_, T>
pub fn get_row(&self, index_key: usize) -> FrameRowView<'_, T>
Returns an immutable view of the row for the given integer key.
Panics if the key is invalid or if the index type is Date
.
Sourcepub fn get_row_mut(&mut self, index_key: usize) -> FrameRowViewMut<'_, T>
pub fn get_row_mut(&mut self, index_key: usize) -> FrameRowViewMut<'_, T>
Returns a mutable view of the row for the given integer key.
Panics if the key is invalid or if the index type is Date
.
Sourcepub fn get_row_date(&self, index_key: NaiveDate) -> FrameRowView<'_, T>
pub fn get_row_date(&self, index_key: NaiveDate) -> FrameRowView<'_, T>
Returns an immutable view of the row for the given date key.
Panics if the key is invalid or if the index type is not Date
.
Sourcepub fn get_row_date_mut(
&mut self,
index_key: NaiveDate,
) -> FrameRowViewMut<'_, T>
pub fn get_row_date_mut( &mut self, index_key: NaiveDate, ) -> FrameRowViewMut<'_, T>
Returns a mutable view of the row for the given date key.
Panics if the key is invalid or if the index type is not Date
.
Sourcepub fn rename<L: Into<String>>(&mut self, old: &str, new: L)
pub fn rename<L: Into<String>>(&mut self, old: &str, new: L)
Renames an existing column. Panics if the source name is missing or if the target name is already in use.
Sourcepub fn add_column<L: Into<String>>(&mut self, name: L, column_data: Vec<T>)
pub fn add_column<L: Into<String>>(&mut self, name: L, column_data: Vec<T>)
Adds a new column at the end of the frame. Panics if the column name exists or if the data length mismatches the row count.
Sourcepub fn delete_column(&mut self, name: &str) -> Vec<T>
pub fn delete_column(&mut self, name: &str) -> Vec<T>
Deletes a column by name and returns its data. Panics if the column name is not found.
Sourcepub fn transpose(&self) -> Matrix<T>
pub fn transpose(&self) -> Matrix<T>
Returns a new Matrix
that is the transpose of the current frame’s matrix.
Sourcepub fn sort_columns(&mut self)
pub fn sort_columns(&mut self)
Sorts columns alphabetically by name, preserving data associations.
pub fn frame_map(&self, f: impl Fn(&T) -> T) -> Frame<T>
pub fn frame_zip(&self, other: &Frame<T>, f: impl Fn(&T, &T) -> T) -> Frame<T>
Trait Implementations§
Source§impl BoolOps for Frame<bool>
impl BoolOps for Frame<bool>
Source§fn apply_axis<U, F>(&self, axis: Axis, f: F) -> Vec<U>
fn apply_axis<U, F>(&self, axis: Axis, f: F) -> Vec<U>
f
to every column/row and collect its
result in a Vec
.fn any_vertical(&self) -> Vec<bool>
fn any_horizontal(&self) -> Vec<bool>
fn all_vertical(&self) -> Vec<bool>
fn all_horizontal(&self) -> Vec<bool>
fn count_vertical(&self) -> Vec<usize>
fn count_horizontal(&self) -> Vec<usize>
fn any(&self) -> bool
fn all(&self) -> bool
fn count(&self) -> usize
Source§impl<T: Clone + PartialEq + Debug> Debug for Frame<T>
A data frame - a Matrix with string-identified columns and a typed row index.
impl<T: Clone + PartialEq + Debug> Debug for Frame<T>
A data frame - a Matrix with string-identified columns and a typed row index.
Frame
extends the concept of a Matrix
by adding named columns
and an index for rows, which can be integers, dates, or a default range.
It allows accessing data by column name (using []
) and by row index value
(using get_row
, get_row_mut
, get_row_date
, get_row_date_mut
methods).
Direct row indexing with frame[row_key]
is not supported due to Rust’s
lifetime and ownership rules clashing with the std::ops::Index
trait when
returning row views or temporary copies. Use the explicit get_row*
methods instead.
The internal data is stored column-major in a Matrix<T>
.
§Type Parameters
T
: The data type of the elements within the frame. Must beClone + PartialEq
. Numerical/Boolean traits required for specific operations (e.g.,Add
,BitAnd
).
§Examples
use rustframe::frame::{Frame, RowIndex};
use rustframe::matrix::Matrix; // Assume Matrix is available
use chrono::NaiveDate;
// Helper fn for dates
fn d(y: i32, m: u32, d: u32) -> NaiveDate { NaiveDate::from_ymd_opt(y,m,d).unwrap() }
// --- Example 1: Basic Creation and Access ---
let matrix_f64 = Matrix::from_cols(vec![
vec![1.0, 2.0, 3.0], // Column "A"
vec![4.0, 5.0, 6.0], // Column "B"
]);
let mut frame1 = Frame::new(matrix_f64, vec!["A", "B"], None);
assert_eq!(frame1.columns(), &["A", "B"]);
assert_eq!(frame1["A"], vec![1.0, 2.0, 3.0]); // Compare with slice literal
assert_eq!(frame1.index(), &RowIndex::Range(0..3));
let row0 = frame1.get_row(0);
assert_eq!(row0["A"], 1.0);
assert_eq!(row0[1], 4.0); // Column "B"
// --- Example 2: Date Index and Mutation ---
let dates = vec![d(2024, 1, 1), d(2024, 1, 2)];
let matrix_string = Matrix::from_cols(vec![ vec!["X".to_string(), "Y".to_string()], ]);
let mut frame2 = Frame::new(matrix_string, vec!["Label"], Some(RowIndex::Date(dates.clone())));
assert_eq!(frame2.index(), &RowIndex::Date(dates));
assert_eq!(frame2.get_row_date(d(2024, 1, 2))["Label"], "Y");
frame2.get_row_date_mut(d(2024, 1, 1)).set_by_index(0, "Z".to_string());
assert_eq!(frame2["Label"], vec!["Z", "Y"]);
// --- Example 3: Element-wise Addition ---
let m1 = Matrix::from_cols(vec![ vec![1.0, 2.0], vec![3.0, 4.0] ]);
let f1 = Frame::new(m1, vec!["C1", "C2"], None);
let m2 = Matrix::from_cols(vec![ vec![0.1, 0.2], vec![0.3, 0.4] ]);
let f2 = Frame::new(m2, vec!["C1", "C2"], None);
let f_sum = &f1 + &f2;
assert_eq!(f_sum["C1"], vec![1.1, 2.2]);
assert_eq!(f_sum["C2"], vec![3.3, 4.4]);
assert_eq!(f_sum.index(), &RowIndex::Range(0..2));
// --- Example 4: Element-wise Multiplication ---
let f_prod = &f1 * &f2;
assert!((f_prod["C1"][0] - 0.1_f64).abs() < 1e-9);
assert!((f_prod["C1"][1] - 0.4_f64).abs() < 1e-9);
assert!((f_prod["C2"][0] - 0.9_f64).abs() < 1e-9);
assert!((f_prod["C2"][1] - 1.6_f64).abs() < 1e-9);
// --- Example 5: Column Manipulation and Sorting ---
let mut frame_manip = Frame::new(
Matrix::from_cols(vec![ vec![1, 2], vec![3, 4] ]), // Example uses i32
vec!["DataC", "DataA"], // Column names (out of order)
None
);
assert_eq!(frame_manip["DataC"], vec![1, 2]);
assert_eq!(frame_manip["DataA"], vec![3, 4]);
frame_manip.add_column("DataB", vec![5, 6]);
assert_eq!(frame_manip.columns(), &["DataC", "DataA", "DataB"]);
frame_manip.rename("DataA", "DataX"); // Rename A -> X
assert_eq!(frame_manip.columns(), &["DataC", "DataX", "DataB"]);
assert_eq!(frame_manip["DataX"], vec![3, 4]); // X has A's original data
let deleted_c = frame_manip.delete_column("DataC");
assert_eq!(deleted_c, vec![1, 2]);
assert_eq!(frame_manip.columns(), &["DataX", "DataB"]);
frame_manip.sort_columns();
assert_eq!(frame_manip.columns(), &["DataB", "DataX"]);
assert_eq!(frame_manip["DataB"], vec![5, 6]);
assert_eq!(frame_manip["DataX"], vec![3, 4]);
Source§impl<T: Clone + PartialEq> Index<&str> for Frame<T>
Enables immutable access to a column’s data via frame["col_name"]
.
impl<T: Clone + PartialEq> Index<&str> for Frame<T>
Enables immutable access to a column’s data via frame["col_name"]
.
Source§impl<T: Clone + PartialEq> IndexMut<&str> for Frame<T>
Enables mutable access to a column’s data via frame["col_name"]
.
impl<T: Clone + PartialEq> IndexMut<&str> for Frame<T>
Enables mutable access to a column’s data via frame["col_name"]
.
Source§impl SeriesOps for Frame<f64>
impl SeriesOps for Frame<f64>
Source§fn apply_axis<U, F>(&self, axis: Axis, f: F) -> Vec<U>
fn apply_axis<U, F>(&self, axis: Axis, f: F) -> Vec<U>
f
to every column/row and collect its
result in a Vec
.