1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
pub mod dataframe {
// Dataframe functions ----------------------------------------------------------------------------
/// A dataframe struct similar to pythons pandas dataframe but with less functionality and only for integers
/// The dataframe is stored as a vector of vectors
/// The first vector contains the column names
/// The rest of the vectors contain the data
/// The data is stored as a vector of vectors
///
/// # Example
/// ```
/// use numbers_rus::dataframe::dataframe::dataframe::DataFrame;
/// let mut df = DataFrame::new();
/// df.add_column("a", &vec![1, 2, 3]);
/// df.add_column("b", &vec![4, 5, 6]);
/// df.add_column("c", &vec![7, 8, 9]);
/// ```
///
pub struct DataFrame {
columns: Vec<String>,
data: Vec<Vec<i128>>,
}
impl DataFrame {
/// Creates a new dataframe
pub fn new() -> DataFrame {
DataFrame {
columns: Vec::new(),
data: Vec::new(),
}
}
/// Adds a column to the dataframe
pub fn add_column(&mut self, name: &str, data: &Vec<i128>) {
self.columns.push(name.to_string());
self.data.push(data.clone());
}
/// Returns the column names
pub fn get_columns(&self) -> Vec<String> {
self.columns.clone()
}
/// Returns the data
pub fn get_data(&self) -> Vec<Vec<i128>> {
self.data.clone()
}
/// Returns the column names
pub fn get_column(&self, name: &str) -> Result<Vec<i128>, &'static str> {
let mut result = Vec::new();
for i in 0..self.columns.len() {
if self.columns[i] == name {
result = self.data[i].clone();
}
}
if result.len() == 0 {
return Err("Column not found");
}
Ok(result)
}
/// Returns the column names
pub fn get_column_index(&self, name: &str) -> Result<i128, &'static str> {
let mut result = 0;
for i in 0..self.columns.len() {
if self.columns[i] == name {
result = i;
}
}
if result == 0 {
return Err("Column not found");
}
Ok(result as i128)
}
/// Returns the column names
pub fn get_column_name(&self, index: i128) -> Result<String, &'static str> {
if index > self.columns.len() as i128 {
return Err("Column not found");
}
Ok(self.columns[index as usize].clone())
}
/// Returns the column names
pub fn get_column_count(&self) -> i128 {
self.columns.len() as i128
}
/// Returns the column names
pub fn get_row_count(&self) -> usize {
self.data[0].len()
}
}
/// Creates a new dataframe
pub fn dataframe_create(
columns: Vec<String>,
data: Vec<Vec<i128>>,
) -> Result<DataFrame, &'static str> {
if columns.len() != data.len() {
return Err("Column count does not match data count");
}
for i in 0..data.len() {
if data[i].len() != data[0].len() {
return Err("Data length does not match");
}
}
Ok(DataFrame {
columns,
data,
})
}
#[cfg(test)]
mod test_dataframe_create {
use super::*;
#[test]
fn it_works() {
let result = dataframe_create(
vec!["a".to_string(), "b".to_string(), "c".to_string()],
vec![vec![1, 2, 3], vec![1, 2, 3], vec![1, 2, 3]],
);
assert_eq!(result.is_ok(), true);
}
#[test]
fn display_dataframe() {
let result = dataframe_create(
vec!["a".to_string(), "b".to_string(), "c".to_string()],
vec![vec![1, 2, 3], vec![1, 2, 3], vec![1, 2, 3]],
);
assert_eq!(
result.unwrap().get_columns(),
vec!["a".to_string(), "b".to_string(), "c".to_string()]
);
}
}
}