17 Commits

Author SHA1 Message Date
Palash Tyagi
e7e1aa5603 Merge eb4fefe363 into 6e0ea441e4 2025-07-02 23:45:47 +01:00
Palash Tyagi
eb4fefe363 Enhance DataFrame display: implement column ellipsis for large datasets; improve row and column index calculations for better output formatting. 2025-07-02 23:45:43 +01:00
Palash Tyagi
60cc97e702 Enhance DataFrame display: implement row truncation with ellipsis for large datasets; improve column width calculations and formatting for better readability. 2025-07-02 23:33:34 +01:00
Palash Tyagi
7e2a5ec18d Enhance DataFrame display: update head and tail methods for improved row retrieval and formatting; refine display output for empty DataFrames and adjust column width calculations. 2025-07-02 22:18:09 +01:00
Palash Tyagi
4038d25b07 applied formatting 2025-07-02 00:25:45 +01:00
Palash Tyagi
aa15248b58 Rename variable for clarity in DataFrame display formatting 2025-07-02 00:25:31 +01:00
Palash Tyagi
fa392ec631 Add head_n and tail_n methods to DataFrame for row retrieval; enhance display formatting 2025-07-02 00:22:52 +01:00
Palash Tyagi
8b6f16236a Refactor TypedFrame methods using macros for common functionality and improve column accessors 2025-07-01 23:26:57 +01:00
Palash Tyagi
58acea8467 Add DataFrame usage examples to README.md 2025-06-22 21:16:06 +01:00
Palash Tyagi
2607d9c3b0 Add pub use statement for DataFrame, DataFrameColumn, and TypedFrame in mod.rs 2025-06-22 21:15:12 +01:00
Palash Tyagi
57ed06f79b Reimplemented dataframe class with TypedFrame interface 2025-06-22 19:47:12 +01:00
Palash Tyagi
01a132264f Remove unused imports and clean up test module in DataFrame implementation 2025-06-22 05:44:24 +01:00
Palash Tyagi
ff4535c56b Implement column renaming in DataFrame, updating both logical names and underlying Frame references. 2025-06-22 05:35:48 +01:00
9b480e8130 Merge branch 'main' into dataframe 2025-06-22 05:22:06 +01:00
Palash Tyagi
fe666a4ddb First draft: Implement DataFrame and DataFrameColumn structures 2025-06-22 05:01:19 +01:00
Palash Tyagi
b80d5ab381 Add documentation for the DataFrame module and include it in the library 2025-06-22 05:00:59 +01:00
Palash Tyagi
49f7558225 Enhance column access methods to clarify usage by name and physical index 2025-06-22 05:00:42 +01:00
5 changed files with 1786 additions and 2 deletions

View File

@@ -98,3 +98,101 @@ assert!(check);
```
---
## DataFrame Usage Example
```rust
use rustframe::{
dataframe::{DataFrame, TypedFrame, DataFrameColumn},
frame::{Frame, RowIndex},
matrix::Matrix,
};
// Helper to create a simple f64 TypedFrame (similar to test helpers)
fn create_f64_typed_frame(name: &str, data: Vec<f64>, index: Option<RowIndex>) -> TypedFrame {
let rows = data.len();
let matrix = Matrix::from_cols(vec![data]);
let frame_index = index.unwrap_or(RowIndex::Range(0..rows));
TypedFrame::F64(Frame::new(
matrix,
vec![name.to_string()],
Some(frame_index),
))
}
// Helper to create a simple i64 TypedFrame
fn create_i64_typed_frame(name: &str, data: Vec<i64>, index: Option<RowIndex>) -> TypedFrame {
let rows = data.len();
let matrix = Matrix::from_cols(vec![data]);
let frame_index = index.unwrap_or(RowIndex::Range(0..rows));
TypedFrame::I64(Frame::new(
matrix,
vec![name.to_string()],
Some(frame_index),
))
}
// Helper to create a simple String TypedFrame
fn create_string_typed_frame(
name: &str,
data: Vec<String>,
index: Option<RowIndex>,
) -> TypedFrame {
let rows = data.len();
let matrix = Matrix::from_cols(vec![data]);
let frame_index = index.unwrap_or(RowIndex::Range(0..rows));
TypedFrame::String(Frame::new(
matrix,
vec![name.to_string()],
Some(frame_index),
))
}
fn main() {
// 1. Create a DataFrame with different data types
let col_a = create_f64_typed_frame("A", vec![1.0, 2.0, 3.0], None);
let col_b = create_i64_typed_frame("B", vec![10, 20, 30], None);
let col_c = create_string_typed_frame(
"C",
vec!["apple".to_string(), "banana".to_string(), "cherry".to_string()],
None,
);
let mut df = DataFrame::new(
vec![col_a, col_b, col_c],
vec!["A".to_string(), "B".to_string(), "C".to_string()],
None,
);
println!("Initial DataFrame:\n{:?}", df);
println!("Columns: {:?}", df.columns());
println!("Rows: {}", df.rows());
// 2. Accessing columns
if let DataFrameColumn::F64(col_a_data) = df.column("A") {
println!("Column 'A' (f64): {:?}", col_a_data);
}
if let DataFrameColumn::String(col_c_data) = df.column("C") {
println!("Column 'C' (String): {:?}", col_c_data);
}
// 3. Add a new column
let new_col_d = create_f64_typed_frame("D", vec![100.0, 200.0, 300.0], None);
df.add_column("D".to_string(), new_col_d);
println!("\nDataFrame after adding column 'D':\n{:?}", df);
println!("Columns after add: {:?}", df.columns());
// 4. Rename a column
df.rename_column("A", "Alpha".to_string());
println!("\nDataFrame after renaming 'A' to 'Alpha':\n{:?}", df);
println!("Columns after rename: {:?}", df.columns());
// 5. Delete a column
let _deleted_col_b = df.delete_column("B");
println!("\nDataFrame after deleting column 'B':\n{:?}", df);
println!("Columns after delete: {:?}", df.columns());
}
```

1667
src/dataframe/df.rs Normal file

File diff suppressed because it is too large Load Diff

4
src/dataframe/mod.rs Normal file
View File

@@ -0,0 +1,4 @@
//! This module provides the DataFrame structure for handling tabular data with mixed types.
pub mod df;
pub use df::{DataFrame, DataFrameColumn, TypedFrame};

View File

@@ -316,7 +316,7 @@ impl<T: Clone + PartialEq> Frame<T> {
)
}
/// Returns an immutable slice of the specified column's data.
/// Returns an immutable slice of the specified column's data by name.
/// Panics if the column name is not found.
pub fn column(&self, name: &str) -> &[T] {
let idx = self
@@ -325,7 +325,13 @@ impl<T: Clone + PartialEq> Frame<T> {
self.matrix.column(idx)
}
/// Returns a mutable slice of the specified column's data.
/// Returns an immutable slice of the specified column's data by its physical index.
/// Panics if the index is out of bounds.
pub fn column_by_physical_idx(&self, idx: usize) -> &[T] {
self.matrix.column(idx)
}
/// Returns a mutable slice of the specified column's data by name.
/// Panics if the column name is not found.
pub fn column_mut(&mut self, name: &str) -> &mut [T] {
let idx = self
@@ -334,6 +340,12 @@ impl<T: Clone + PartialEq> Frame<T> {
self.matrix.column_mut(idx)
}
/// Returns a mutable slice of the specified column's data by its physical index.
/// Panics if the index is out of bounds.
pub fn column_mut_by_physical_idx(&mut self, idx: usize) -> &mut [T] {
self.matrix.column_mut(idx)
}
// Row access methods
/// Returns an immutable view of the row for the given integer key.

View File

@@ -1,5 +1,8 @@
#![doc = include_str!("../README.md")]
/// Documentation for the [`crate::dataframe`] module.
pub mod dataframe;
/// Documentation for the [`crate::matrix`] module.
pub mod matrix;