-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
142 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use crate::prelude::*; | ||
use polars_time::{ | ||
Duration, Window | ||
}; | ||
use crate::frame::groupby::{ | ||
GroupTuples, | ||
GroupBy | ||
}; | ||
|
||
|
||
#[derive(Clone, Debug)] | ||
pub struct DynamicGroupOptions { | ||
pub time_column: String, | ||
/// start a window at this interval | ||
every: Duration, | ||
/// window duration | ||
period: Duration, | ||
/// offset window boundaries | ||
offset: Duration, | ||
/// truncate the time column values to the window | ||
truncate: bool | ||
} | ||
|
||
impl DataFrame { | ||
pub fn groupby_dynamic(&self, options: &DynamicGroupOptions) -> Result<(Self, GroupTuples)> { | ||
let w = Window::new(options.every, options.period, options.offset); | ||
|
||
|
||
let time = self.column(&options.time_column)?; | ||
if time.null_count() > 0 { | ||
panic!("null values in dynamic groupby not yet supported, fill nulls.") | ||
} | ||
|
||
let dt = time.cast(&DataType::Datetime)?; | ||
let dt = dt.datetime().unwrap(); | ||
|
||
let gt = dt.downcast_iter().map(|vals| { | ||
let ts = vals.values().as_slice(); | ||
polars_time::groupby::groupby(w, ts) | ||
}).flatten().collect::<Vec<_>>(); | ||
|
||
let mut df = self.clone(); | ||
if options.truncate { | ||
let out = dt.apply(|v| w.truncate(v)); | ||
let out = out.cast(&DataType::Datetime).unwrap(); | ||
df.with_column(out)?; | ||
} | ||
|
||
|
||
Ok((df, gt)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
polars/polars-lazy/src/physical_plan/executors/groupby_dynamic.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use super::*; | ||
use crate::logical_plan::Context; | ||
use crate::prelude::utils::as_aggregated; | ||
use crate::utils::rename_aexpr_root_name; | ||
use polars_core::utils::{accumulate_dataframes_vertical, split_df}; | ||
use polars_core::POOL; | ||
use rayon::prelude::*; | ||
use polars_core::frame::groupby::DynamicGroupOptions; | ||
|
||
pub struct GroupByDynamicExec { | ||
input: Box<dyn Executor>, | ||
keys: Vec<Arc<dyn PhysicalExpr>>, | ||
aggs: Vec<Arc<dyn PhysicalExpr>>, | ||
options: DynamicGroupOptions | ||
} | ||
|
||
impl Executor for GroupByDynamicExec { | ||
fn execute(&mut self, state: &ExecutionState) -> Result<DataFrame> { | ||
let df = self.input.execute(state)?; | ||
|
||
let (df, groups) = df.groupby_dynamic(&self.options)?; | ||
let key = df.column(&self.options.time_column)?.clone(); | ||
|
||
let agg_columns = POOL.install(|| { | ||
self.aggs | ||
.par_iter() | ||
.map(|expr| { | ||
let opt_agg = as_aggregated(expr.as_ref(), &df, &groups, state)?; | ||
if let Some(agg) = &opt_agg { | ||
if agg.len() != groups.len() { | ||
return Err(PolarsError::ComputeError( | ||
format!("returned aggregation is a different length: {} than the group lengths: {}", | ||
agg.len(), | ||
groups.len()).into() | ||
)) | ||
} | ||
}; | ||
Ok(opt_agg) | ||
}) | ||
.collect::<Result<Vec<_>>>() | ||
|
||
})?; | ||
|
||
let mut columns= Vec::with_capacity(agg_columns.len() + 1); | ||
columns.push(key); | ||
columns.extend(agg_columns.into_iter().flatten()); | ||
|
||
DataFrame::new(columns) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters