mirror of
https://github.com/nushell/nushell.git
synced 2025-04-15 08:48:19 +02:00
Merge 5c52e68d71
into 885b87a842
This commit is contained in:
commit
42d0e9a36f
@ -10,6 +10,7 @@ mod median;
|
||||
mod min;
|
||||
mod n_null;
|
||||
mod n_unique;
|
||||
mod over;
|
||||
mod quantile;
|
||||
mod rolling;
|
||||
mod std;
|
||||
@ -30,6 +31,7 @@ use mean::ExprMean;
|
||||
use min::ExprMin;
|
||||
pub use n_null::NNull;
|
||||
pub use n_unique::NUnique;
|
||||
pub use over::Over;
|
||||
pub use rolling::Rolling;
|
||||
use std::ExprStd;
|
||||
pub use sum::ExprSum;
|
||||
@ -52,6 +54,7 @@ pub(crate) fn aggregation_commands() -> Vec<Box<dyn PluginCommand<Plugin = Polar
|
||||
Box::new(median::LazyMedian),
|
||||
Box::new(quantile::LazyQuantile),
|
||||
Box::new(groupby::ToLazyGroupBy),
|
||||
Box::new(Over),
|
||||
Box::new(Rolling),
|
||||
Box::new(ValueCount),
|
||||
Box::new(NNull),
|
||||
|
@ -0,0 +1,119 @@
|
||||
use crate::{
|
||||
dataframe::values::{NuDataFrame, NuExpression},
|
||||
values::{cant_convert_err, CustomValueSupport, PolarsPluginObject, PolarsPluginType},
|
||||
PolarsPlugin,
|
||||
};
|
||||
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand};
|
||||
use nu_protocol::{
|
||||
Category, Example, LabeledError, PipelineData, Signature, Span, SyntaxShape, Type, Value,
|
||||
};
|
||||
use polars::df;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Over;
|
||||
|
||||
impl PluginCommand for Over {
|
||||
type Plugin = PolarsPlugin;
|
||||
|
||||
fn name(&self) -> &str {
|
||||
"polars over"
|
||||
}
|
||||
|
||||
fn description(&self) -> &str {
|
||||
"Compute expressions over a window group defined by partition expressions."
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build(self.name())
|
||||
.rest(
|
||||
"partition by expressions",
|
||||
SyntaxShape::Any,
|
||||
"Expression(s) that define the partition window",
|
||||
)
|
||||
.input_output_type(
|
||||
Type::Custom("expression".into()),
|
||||
Type::Custom("expression".into()),
|
||||
)
|
||||
.category(Category::Custom("lazyframe".into()))
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![
|
||||
Example {
|
||||
description: "Compute expression over an aggregation window",
|
||||
example: r#"[[a b]; [x 2] [x 4] [y 6] [y 4]]
|
||||
| polars into-lazy
|
||||
| polars select a (polars col b | polars cumulative sum | polars over a | polars as cum_b)
|
||||
| polars collect"#,
|
||||
result: Some(
|
||||
NuDataFrame::from(
|
||||
df!(
|
||||
"a" => &["x", "x", "y", "y"],
|
||||
"cum_b" => &[2, 6, 6, 10]
|
||||
)
|
||||
.expect("should not fail"),
|
||||
)
|
||||
.into_value(Span::test_data()),
|
||||
),
|
||||
},
|
||||
Example {
|
||||
description: "Compute expression over an aggregation window where partitions are defined by expressions",
|
||||
example: r#"[[a b]; [x 2] [X 4] [Y 6] [y 4]]
|
||||
| polars into-lazy
|
||||
| polars select a (polars col b | polars cumulative sum | polars over (polars col a | polars lowercase) | polars as cum_b)
|
||||
| polars collect"#,
|
||||
result: Some(
|
||||
NuDataFrame::from(
|
||||
df!(
|
||||
"a" => &["x", "X", "Y", "y"],
|
||||
"cum_b" => &[2, 6, 6, 10]
|
||||
)
|
||||
.expect("should not fail"),
|
||||
)
|
||||
.into_value(Span::test_data()),
|
||||
),
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
plugin: &Self::Plugin,
|
||||
engine: &EngineInterface,
|
||||
call: &EvaluatedCall,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, LabeledError> {
|
||||
let vals: Vec<Value> = call.rest(0)?;
|
||||
let expr_value = Value::list(vals, call.head);
|
||||
let expressions = NuExpression::extract_exprs(plugin, expr_value)?;
|
||||
|
||||
let input_value = input.into_value(call.head)?;
|
||||
|
||||
match PolarsPluginObject::try_from_value(plugin, &input_value)? {
|
||||
PolarsPluginObject::NuExpression(expr) => {
|
||||
let expr: NuExpression = expr
|
||||
.into_polars()
|
||||
.over_with_options(expressions, None, Default::default())
|
||||
.into();
|
||||
expr.to_pipeline_data(plugin, engine, call.head)
|
||||
}
|
||||
_ => Err(cant_convert_err(
|
||||
&input_value,
|
||||
&[PolarsPluginType::NuExpression],
|
||||
)),
|
||||
}
|
||||
.map_err(LabeledError::from)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use crate::test::test_polars_plugin_command;
|
||||
use nu_protocol::ShellError;
|
||||
|
||||
#[test]
|
||||
fn test_examples() -> Result<(), ShellError> {
|
||||
test_polars_plugin_command(&Over)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user