mirror of
https://github.com/Magnus167/rustframe.git
synced 2025-08-21 10:40:01 +00:00
Compare commits
1 Commits
f2ba7a4b3b
...
10f9793e26
Author | SHA1 | Date | |
---|---|---|---|
![]() |
10f9793e26 |
62
README.md
62
README.md
@ -98,17 +98,15 @@ assert!(check);
|
|||||||
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## DataFrame Usage Example
|
## DataFrame Usage Example
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
use chrono::NaiveDate;
|
|
||||||
use rustframe::dataframe::DataFrame;
|
use rustframe::dataframe::DataFrame;
|
||||||
use rustframe::utils::{BDateFreq, BDatesList};
|
use chrono::NaiveDate;
|
||||||
use std::any::TypeId;
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::any::TypeId; // Required for checking TypeId
|
||||||
|
|
||||||
// Helper for NaiveDate
|
// Helper for NaiveDate
|
||||||
fn d(y: i32, m: u32, d: u32) -> NaiveDate {
|
fn d(y: i32, m: u32, d: u32) -> NaiveDate {
|
||||||
@ -121,49 +119,25 @@ let mut df = DataFrame::new();
|
|||||||
// Add columns of different types
|
// Add columns of different types
|
||||||
df.add_column("col_int1", vec![1, 2, 3, 4, 5]);
|
df.add_column("col_int1", vec![1, 2, 3, 4, 5]);
|
||||||
df.add_column("col_float1", vec![1.1, 2.2, 3.3, 4.4, 5.5]);
|
df.add_column("col_float1", vec![1.1, 2.2, 3.3, 4.4, 5.5]);
|
||||||
df.add_column(
|
df.add_column("col_string", vec!["apple".to_string(), "banana".to_string(), "cherry".to_string(), "date".to_string(), "elderberry".to_string()]);
|
||||||
"col_string",
|
|
||||||
vec![
|
|
||||||
"apple".to_string(),
|
|
||||||
"banana".to_string(),
|
|
||||||
"cherry".to_string(),
|
|
||||||
"date".to_string(),
|
|
||||||
"elderberry".to_string(),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
df.add_column("col_bool", vec![true, false, true, false, true]);
|
df.add_column("col_bool", vec![true, false, true, false, true]);
|
||||||
// df.add_column("col_date", vec![d(2023,1,1), d(2023,1,2), d(2023,1,3), d(2023,1,4), d(2023,1,5)]);
|
df.add_column("col_date", vec![d(2023,1,1), d(2023,1,2), d(2023,1,3), d(2023,1,4), d(2023,1,5)]);
|
||||||
df.add_column(
|
|
||||||
"col_date",
|
|
||||||
BDatesList::from_n_periods("2023-01-01".to_string(), BDateFreq::Daily, 5)
|
|
||||||
.unwrap()
|
|
||||||
.list()
|
|
||||||
.unwrap(),
|
|
||||||
);
|
|
||||||
|
|
||||||
println!("DataFrame after initial column additions:\n{}", df);
|
println!("DataFrame after initial column additions:\n{}", df);
|
||||||
|
|
||||||
// Demonstrate frame re-use when adding columns of existing types
|
// Demonstrate frame re-use when adding columns of existing types
|
||||||
let initial_frames_count = df.num_internal_frames();
|
let initial_frames_count = df.num_internal_frames();
|
||||||
println!(
|
println!("\nInitial number of internal frames: {}", initial_frames_count);
|
||||||
"\nInitial number of internal frames: {}",
|
|
||||||
initial_frames_count
|
|
||||||
);
|
|
||||||
|
|
||||||
df.add_column("col_int2", vec![6, 7, 8, 9, 10]);
|
df.add_column("col_int2", vec![6, 7, 8, 9, 10]);
|
||||||
df.add_column("col_float2", vec![6.6, 7.7, 8.8, 9.9, 10.0]);
|
df.add_column("col_float2", vec![6.6, 7.7, 8.8, 9.9, 10.0]);
|
||||||
|
|
||||||
let frames_after_reuse = df.num_internal_frames();
|
let frames_after_reuse = df.num_internal_frames();
|
||||||
println!(
|
println!("Number of internal frames after adding more columns of existing types: {}", frames_after_reuse);
|
||||||
"Number of internal frames after adding more columns of existing types: {}",
|
|
||||||
frames_after_reuse
|
|
||||||
);
|
|
||||||
assert_eq!(initial_frames_count, frames_after_reuse); // Should be equal, demonstrating re-use
|
assert_eq!(initial_frames_count, frames_after_reuse); // Should be equal, demonstrating re-use
|
||||||
|
|
||||||
println!(
|
println!("\nDataFrame after adding more columns of existing types:\n{}", df);
|
||||||
"\nDataFrame after adding more columns of existing types:\n{}",
|
|
||||||
df
|
|
||||||
);
|
|
||||||
|
|
||||||
// Get number of rows and columns
|
// Get number of rows and columns
|
||||||
println!("Rows: {}", df.rows()); // Output: Rows: 5
|
println!("Rows: {}", df.rows()); // Output: Rows: 5
|
||||||
@ -175,21 +149,17 @@ println!("Column names: {:?}", df.get_column_names());
|
|||||||
|
|
||||||
// Get a specific column by name and type
|
// Get a specific column by name and type
|
||||||
let int_col = df.get_column::<i32>("col_int1").unwrap();
|
let int_col = df.get_column::<i32>("col_int1").unwrap();
|
||||||
// Output: Integer column: [1, 2, 3, 4, 5]
|
println!("Integer column (col_int1): {:?}", int_col); // Output: Integer column: [1, 2, 3, 4, 5]
|
||||||
println!("Integer column (col_int1): {:?}", int_col);
|
|
||||||
|
|
||||||
let int_col2 = df.get_column::<i32>("col_int2").unwrap();
|
let int_col2 = df.get_column::<i32>("col_int2").unwrap();
|
||||||
// Output: Integer column: [6, 7, 8, 9, 10]
|
println!("Integer column (col_int2): {:?}", int_col2); // Output: Integer column: [6, 7, 8, 9, 10]
|
||||||
println!("Integer column (col_int2): {:?}", int_col2);
|
|
||||||
|
|
||||||
let float_col = df.get_column::<f64>("col_float1").unwrap();
|
let float_col = df.get_column::<f64>("col_float1").unwrap();
|
||||||
// Output: Float column: [1.1, 2.2, 3.3, 4.4, 5.5]
|
println!("Float column (col_float1): {:?}", float_col); // Output: Float column: [1.1, 2.2, 3.3, 4.4, 5.5]
|
||||||
println!("Float column (col_float1): {:?}", float_col);
|
|
||||||
|
|
||||||
// Attempt to get a column with incorrect type (returns None)
|
// Attempt to get a column with incorrect type (returns None)
|
||||||
let wrong_type_col = df.get_column::<bool>("col_int1");
|
let wrong_type_col = df.get_column::<bool>("col_int1");
|
||||||
// Output: Wrong type column: None
|
println!("Wrong type column: {:?}", wrong_type_col); // Output: Wrong type column: None
|
||||||
println!("Wrong type column: {:?}", wrong_type_col);
|
|
||||||
|
|
||||||
// Get a row by index
|
// Get a row by index
|
||||||
let row_0 = df.get_row(0).unwrap();
|
let row_0 = df.get_row(0).unwrap();
|
||||||
@ -202,8 +172,7 @@ println!("Row 2: {:?}", row_2);
|
|||||||
|
|
||||||
// Attempt to get an out-of-bounds row (returns None)
|
// Attempt to get an out-of-bounds row (returns None)
|
||||||
let row_out_of_bounds = df.get_row(10);
|
let row_out_of_bounds = df.get_row(10);
|
||||||
// Output: Row out of bounds: None
|
println!("Row out of bounds: {:?}", row_out_of_bounds); // Output: Row out of bounds: None
|
||||||
println!("Row out of bounds: {:?}", row_out_of_bounds);
|
|
||||||
|
|
||||||
// Drop a column
|
// Drop a column
|
||||||
df.drop_column("col_bool");
|
df.drop_column("col_bool");
|
||||||
@ -217,10 +186,7 @@ df.drop_column("col_float1");
|
|||||||
println!("\nDataFrame after dropping 'col_float1':\n{}", df);
|
println!("\nDataFrame after dropping 'col_float1':\n{}", df);
|
||||||
|
|
||||||
println!("Columns after second drop: {}", df.cols());
|
println!("Columns after second drop: {}", df.cols());
|
||||||
println!(
|
println!("Column names after second drop: {:?}", df.get_column_names());
|
||||||
"Column names after second drop: {:?}",
|
|
||||||
df.get_column_names()
|
|
||||||
);
|
|
||||||
|
|
||||||
// Attempt to drop a non-existent column (will panic)
|
// Attempt to drop a non-existent column (will panic)
|
||||||
// df.drop_column("non_existent_col"); // Uncomment to see panic
|
// df.drop_column("non_existent_col"); // Uncomment to see panic
|
||||||
|
Loading…
x
Reference in New Issue
Block a user