Struct Frame

Source
pub struct Frame<T: Clone + PartialEq> { /* private fields */ }

Implementations§

Source§

impl<T: Clone + PartialEq> Frame<T>

Source

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.

Source

pub fn matrix(&self) -> &Matrix<T>

Returns an immutable reference to the underlying Matrix.

Source

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.

Source

pub fn columns(&self) -> &[String]

Returns the list of column names in their current order.

Source

pub fn index(&self) -> &RowIndex

Returns a reference to the frame’s row index.

Source

pub fn rows(&self) -> usize

Returns the number of rows in the frame.

Source

pub fn cols(&self) -> usize

Returns the number of columns in the frame.

Source

pub fn column_index(&self, name: &str) -> Option<usize>

Returns the physical column index for the given column name, if present.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn transpose(&self) -> Matrix<T>

Returns a new Matrix that is the transpose of the current frame’s matrix.

Source

pub fn sort_columns(&mut self)

Sorts columns alphabetically by name, preserving data associations.

Source

pub fn frame_map(&self, f: impl Fn(&T) -> T) -> Frame<T>

Source

pub fn frame_zip(&self, other: &Frame<T>, f: impl Fn(&T, &T) -> T) -> Frame<T>

Trait Implementations§

Source§

impl<'a, 'b, T> Add<&'b Frame<T>> for &'a Frame<T>
where T: Clone + PartialEq + Add<Output = T>,

Source§

type Output = Frame<T>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &'b Frame<T>) -> Frame<T>

Performs the + operation. Read more
Source§

impl<'b, T> Add<&'b Frame<T>> for Frame<T>
where T: Clone + PartialEq + Add<Output = T>,

Source§

type Output = Frame<T>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &'b Frame<T>) -> Frame<T>

Performs the + operation. Read more
Source§

impl<'a, T> Add<Frame<T>> for &'a Frame<T>
where T: Clone + PartialEq + Add<Output = T>,

Source§

type Output = Frame<T>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Frame<T>) -> Frame<T>

Performs the + operation. Read more
Source§

impl<T> Add for Frame<T>
where T: Clone + PartialEq + Add<Output = T>,

Source§

type Output = Frame<T>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Frame<T>) -> Frame<T>

Performs the + operation. Read more
Source§

impl<'a, 'b> BitAnd<&'b Frame<bool>> for &'a Frame<bool>

Source§

type Output = Frame<bool>

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: &'b Frame<bool>) -> Frame<bool>

Performs the & operation. Read more
Source§

impl<'b> BitAnd<&'b Frame<bool>> for Frame<bool>

Source§

type Output = Frame<bool>

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: &'b Frame<bool>) -> Frame<bool>

Performs the & operation. Read more
Source§

impl<'a> BitAnd<Frame<bool>> for &'a Frame<bool>

Source§

type Output = Frame<bool>

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: Frame<bool>) -> Frame<bool>

Performs the & operation. Read more
Source§

impl BitAnd for Frame<bool>

Source§

type Output = Frame<bool>

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: Frame<bool>) -> Frame<bool>

Performs the & operation. Read more
Source§

impl<'a, 'b> BitOr<&'b Frame<bool>> for &'a Frame<bool>

Source§

type Output = Frame<bool>

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: &'b Frame<bool>) -> Frame<bool>

Performs the | operation. Read more
Source§

impl<'b> BitOr<&'b Frame<bool>> for Frame<bool>

Source§

type Output = Frame<bool>

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: &'b Frame<bool>) -> Frame<bool>

Performs the | operation. Read more
Source§

impl<'a> BitOr<Frame<bool>> for &'a Frame<bool>

Source§

type Output = Frame<bool>

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Frame<bool>) -> Frame<bool>

Performs the | operation. Read more
Source§

impl BitOr for Frame<bool>

Source§

type Output = Frame<bool>

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Frame<bool>) -> Frame<bool>

Performs the | operation. Read more
Source§

impl<'a, 'b> BitXor<&'b Frame<bool>> for &'a Frame<bool>

Source§

type Output = Frame<bool>

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: &'b Frame<bool>) -> Frame<bool>

Performs the ^ operation. Read more
Source§

impl<'b> BitXor<&'b Frame<bool>> for Frame<bool>

Source§

type Output = Frame<bool>

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: &'b Frame<bool>) -> Frame<bool>

Performs the ^ operation. Read more
Source§

impl<'a> BitXor<Frame<bool>> for &'a Frame<bool>

Source§

type Output = Frame<bool>

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: Frame<bool>) -> Frame<bool>

Performs the ^ operation. Read more
Source§

impl BitXor for Frame<bool>

Source§

type Output = Frame<bool>

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: Frame<bool>) -> Frame<bool>

Performs the ^ operation. Read more
Source§

impl BoolOps for Frame<bool>

Source§

fn apply_axis<U, F>(&self, axis: Axis, f: F) -> Vec<U>
where F: FnMut(&[bool]) -> U,

Generic helper: apply f to every column/row and collect its result in a Vec.
Source§

fn any_vertical(&self) -> Vec<bool>

Source§

fn any_horizontal(&self) -> Vec<bool>

Source§

fn all_vertical(&self) -> Vec<bool>

Source§

fn all_horizontal(&self) -> Vec<bool>

Source§

fn count_vertical(&self) -> Vec<usize>

Source§

fn count_horizontal(&self) -> Vec<usize>

Source§

fn any(&self) -> bool

Source§

fn all(&self) -> bool

Source§

fn count(&self) -> usize

Source§

impl<T: Clone + Clone + PartialEq> Clone for Frame<T>

Source§

fn clone(&self) -> Frame<T>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

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 be Clone + 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§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, 'b, T> Div<&'b Frame<T>> for &'a Frame<T>
where T: Clone + PartialEq + Div<Output = T>,

Source§

type Output = Frame<T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: &'b Frame<T>) -> Frame<T>

Performs the / operation. Read more
Source§

impl<'b, T> Div<&'b Frame<T>> for Frame<T>
where T: Clone + PartialEq + Div<Output = T>,

Source§

type Output = Frame<T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: &'b Frame<T>) -> Frame<T>

Performs the / operation. Read more
Source§

impl<'a, T> Div<Frame<T>> for &'a Frame<T>
where T: Clone + PartialEq + Div<Output = T>,

Source§

type Output = Frame<T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Frame<T>) -> Frame<T>

Performs the / operation. Read more
Source§

impl<T> Div for Frame<T>
where T: Clone + PartialEq + Div<Output = T>,

Source§

type Output = Frame<T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Frame<T>) -> Frame<T>

Performs the / operation. Read more
Source§

impl<T: Clone + PartialEq> Index<&str> for Frame<T>

Enables immutable access to a column’s data via frame["col_name"].

Source§

type Output = [T]

The returned type after indexing.
Source§

fn index(&self, name: &str) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T: Clone + PartialEq> IndexMut<&str> for Frame<T>

Enables mutable access to a column’s data via frame["col_name"].

Source§

fn index_mut(&mut self, name: &str) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<'a, 'b, T> Mul<&'b Frame<T>> for &'a Frame<T>
where T: Clone + PartialEq + Mul<Output = T>,

Source§

type Output = Frame<T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &'b Frame<T>) -> Frame<T>

Performs the * operation. Read more
Source§

impl<'b, T> Mul<&'b Frame<T>> for Frame<T>
where T: Clone + PartialEq + Mul<Output = T>,

Source§

type Output = Frame<T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &'b Frame<T>) -> Frame<T>

Performs the * operation. Read more
Source§

impl<'a, T> Mul<Frame<T>> for &'a Frame<T>
where T: Clone + PartialEq + Mul<Output = T>,

Source§

type Output = Frame<T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Frame<T>) -> Frame<T>

Performs the * operation. Read more
Source§

impl<T> Mul for Frame<T>
where T: Clone + PartialEq + Mul<Output = T>,

Source§

type Output = Frame<T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Frame<T>) -> Frame<T>

Performs the * operation. Read more
Source§

impl Not for &Frame<bool>

Implements logical NOT (!) for &Frame<bool>, borrowing the frame.

Source§

type Output = Frame<bool>

The resulting type after applying the ! operator.
Source§

fn not(self) -> Frame<bool>

Performs the unary ! operation. Read more
Source§

impl Not for Frame<bool>

Implements logical NOT (!) for Frame<bool>, consuming the frame.

Source§

type Output = Frame<bool>

The resulting type after applying the ! operator.
Source§

fn not(self) -> Frame<bool>

Performs the unary ! operation. Read more
Source§

impl<T: PartialEq + Clone + PartialEq> PartialEq for Frame<T>

Source§

fn eq(&self, other: &Frame<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl SeriesOps for Frame<f64>

Source§

fn apply_axis<U, F>(&self, axis: Axis, f: F) -> Vec<U>
where F: FnMut(&[f64]) -> U,

Generic helper: apply f to every column/row and collect its result in a Vec.
Source§

fn map<F>(&self, f: F) -> FloatMatrix
where F: Fn(f64) -> f64,

Source§

fn zip<F>(&self, other: &Self, f: F) -> FloatMatrix
where F: Fn(f64, f64) -> f64,

Source§

fn matrix_mul(&self, other: &Self) -> FloatMatrix

Source§

fn dot(&self, other: &Self) -> FloatMatrix

Source§

fn sum_vertical(&self) -> Vec<f64>

Source§

fn sum_horizontal(&self) -> Vec<f64>

Source§

fn prod_horizontal(&self) -> Vec<f64>

Source§

fn prod_vertical(&self) -> Vec<f64>

Source§

fn cumsum_horizontal(&self) -> FloatMatrix

Source§

fn cumsum_vertical(&self) -> FloatMatrix

Source§

fn count_nan_vertical(&self) -> Vec<usize>

Source§

fn count_nan_horizontal(&self) -> Vec<usize>

Source§

fn is_nan(&self) -> BoolMatrix

Source§

impl<'a, 'b, T> Sub<&'b Frame<T>> for &'a Frame<T>
where T: Clone + PartialEq + Sub<Output = T>,

Source§

type Output = Frame<T>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &'b Frame<T>) -> Frame<T>

Performs the - operation. Read more
Source§

impl<'b, T> Sub<&'b Frame<T>> for Frame<T>
where T: Clone + PartialEq + Sub<Output = T>,

Source§

type Output = Frame<T>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &'b Frame<T>) -> Frame<T>

Performs the - operation. Read more
Source§

impl<'a, T> Sub<Frame<T>> for &'a Frame<T>
where T: Clone + PartialEq + Sub<Output = T>,

Source§

type Output = Frame<T>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Frame<T>) -> Frame<T>

Performs the - operation. Read more
Source§

impl<T> Sub for Frame<T>
where T: Clone + PartialEq + Sub<Output = T>,

Source§

type Output = Frame<T>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Frame<T>) -> Frame<T>

Performs the - operation. Read more
Source§

impl<T: Clone + PartialEq> StructuralPartialEq for Frame<T>

Auto Trait Implementations§

§

impl<T> Freeze for Frame<T>

§

impl<T> RefUnwindSafe for Frame<T>
where T: RefUnwindSafe,

§

impl<T> Send for Frame<T>
where T: Send,

§

impl<T> Sync for Frame<T>
where T: Sync,

§

impl<T> Unpin for Frame<T>
where T: Unpin,

§

impl<T> UnwindSafe for Frame<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Broadcastable<T> for T
where T: Clone,

Source§

fn to_vec(&self, rows: usize, cols: usize) -> Vec<T>

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.