diff --git a/src/series/num_series.rs b/src/series/num_series.rs index 9c23345..dd53f73 100644 --- a/src/series/num_series.rs +++ b/src/series/num_series.rs @@ -1,21 +1,121 @@ +use super::Series; + pub trait NumSeries { - fn sum(&self) -> f64; - fn product(&self) -> f64; - fn mean(&self) -> f64; - fn median(&self) -> f64; - fn mode(&self) -> f64; - fn std_dev(&self) -> f64; - fn min(&self) -> f64; - - fn add(&self) -> f64; - fn sub(&self) -> f64; - fn mul(&self) -> f64; - fn div(&self) -> f64; - - fn corr(&self) -> f64; // calculates correlation matrix - fn cov(&self) -> f64; // calculates covariance matrix - fn max(&self) -> f64; + fn sum(&self) -> Option; + fn product(&self) -> Option; + fn mean(&self) -> Option; + fn median(&self) -> Option; + fn mode(&self) -> Option; + fn std_dev(&self) -> Option; + fn min(&self) -> Option; + + fn add(&self) -> Option; + fn sub(&self) -> Option; + fn mul(&self) -> Option; + fn div(&self) -> Option; + + fn corr(&self) -> Option; // calculates correlation matrix + fn cov(&self) -> Option; // calculates covariance matrix + fn max(&self) -> Option; fn drop_na(&self); // clears null/empty values fn fill_na(&self); // fill empty values +} + +impl NumSeries for Series { + fn sum(&self) -> Option { + if let Series::Num(ref vec) = &self { + Some(vec.iter().sum::()) + } else { + None + } + } + + fn product(&self) -> Option { + if let Series::Num(ref vec) = &self { + Some(vec.iter().product::()) + } else { + None + } + } + + fn mean(&self) -> Option { + if let Series::Num(ref vec) = &self { + Some(vec.iter().sum::() / vec.len() as f64) + } else { + None + } + } + + fn median(&self) -> Option { + todo!() + } + + fn mode(&self) -> Option { + todo!() + } + + fn std_dev(&self) -> Option { + todo!() + } + + fn min(&self) -> Option { + if let Series::Num(ref vec) = &self { + if vec.len() == 0 { + return None; + } + + let mut vec = vec.clone(); + vec.sort_by(|a, b| a.partial_cmp(b).unwrap()); + Some(vec[0]) + } else { + None + } + } + + fn max(&self) -> Option { + if let Series::Num(ref vec) = &self { + if vec.len() == 0 { + return None; + } + + let mut vec = vec.clone(); + vec.sort_by(|a, b| a.partial_cmp(b).unwrap()); + Some(vec[vec.len() - 1]) + } else { + None + } + } + + fn add(&self) -> Option { + todo!() + } + + fn sub(&self) -> Option { + todo!() + } + + fn mul(&self) -> Option { + todo!() + } + + fn div(&self) -> Option { + todo!() + } + + fn corr(&self) -> Option { + todo!() + } + + fn cov(&self) -> Option { + todo!() + } + + fn drop_na(&self) { + todo!() + } + + fn fill_na(&self) { + todo!() + } } \ No newline at end of file