pub enum ConstOrColumn<T> {
Column(String),
Const(T),
}
pub trait ConstOrColumnTrait<T> {
fn get(&self, _: &DataFrame, _: usize) -> T;
}
impl<T: Clone> ConstOrColumnTrait<T> for ConstOrColumn<T> {
default fn get(&self, _: &DataFrame, _: usize) -> T {
match self {
Self::Column(_) => {
unimplemented!(
"Column of type {} is not implemented",
std::any::type_name::<T>()
);
}
Self::Const(c) => c.clone(),
}
}
}
impl ConstOrColumnTrait<f64> for ConstOrColumn<f64> {
fn get(&self, df: &DataFrame, idx: usize) -> f64 {
match self {
Self::Column(col) => df.column(col).unwrap().f64().unwrap().get(idx).unwrap(),
Self::Const(c) => *c,
}
}
}
impl ConstOrColumn<f64> {
pub fn min_max(&self, df: &DataFrame) -> Option<(f64, f64)> {
match self {
Self::Column(col) => {
let col = df.column(col).unwrap().f64().unwrap();
Some((col.min().unwrap(), col.max().unwrap()))
}
&Self::Const(c) => Some((c, c)),
}
}
}
use polars::prelude::{ChunkAgg, DataFrame, TakeRandom};