mirror of
https://github.com/nushell/nushell.git
synced 2025-04-17 01:38:19 +02:00
Added expression support for polars contains
(#13769)
# Description Adds the ability to use `polars contains` as an expression: <img width="785" alt="Screenshot 2024-09-03 at 14 39 03" src="https://github.com/user-attachments/assets/35c0d4e5-6bef-4974-a31f-463c7203bd03"> # User-Facing Changes - `polars contains` can now be used with expressions
This commit is contained in:
parent
eb0de25d19
commit
63b94dbd28
@ -1,4 +1,9 @@
|
|||||||
use crate::{values::CustomValueSupport, PolarsPlugin};
|
use crate::{
|
||||||
|
values::{
|
||||||
|
cant_convert_err, CustomValueSupport, NuExpression, PolarsPluginObject, PolarsPluginType,
|
||||||
|
},
|
||||||
|
PolarsPlugin,
|
||||||
|
};
|
||||||
|
|
||||||
use super::super::super::values::{Column, NuDataFrame};
|
use super::super::super::values::{Column, NuDataFrame};
|
||||||
|
|
||||||
@ -7,7 +12,7 @@ use nu_protocol::{
|
|||||||
Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type,
|
Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type,
|
||||||
Value,
|
Value,
|
||||||
};
|
};
|
||||||
use polars::prelude::{IntoSeries, StringNameSpaceImpl};
|
use polars::prelude::{lit, IntoSeries, StringNameSpaceImpl};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Contains;
|
pub struct Contains;
|
||||||
@ -30,33 +35,62 @@ impl PluginCommand for Contains {
|
|||||||
SyntaxShape::String,
|
SyntaxShape::String,
|
||||||
"Regex pattern to be searched",
|
"Regex pattern to be searched",
|
||||||
)
|
)
|
||||||
.input_output_type(
|
.input_output_types(vec![
|
||||||
Type::Custom("dataframe".into()),
|
(
|
||||||
Type::Custom("dataframe".into()),
|
Type::Custom("expression".into()),
|
||||||
)
|
Type::Custom("expression".into()),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
Type::Custom("dataframe".into()),
|
||||||
|
Type::Custom("dataframe".into()),
|
||||||
|
),
|
||||||
|
])
|
||||||
.category(Category::Custom("dataframe".into()))
|
.category(Category::Custom("dataframe".into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn examples(&self) -> Vec<Example> {
|
fn examples(&self) -> Vec<Example> {
|
||||||
vec![Example {
|
vec![
|
||||||
description: "Returns boolean indicating if pattern was found",
|
Example {
|
||||||
example: "[abc acb acb] | polars into-df | polars contains ab",
|
description: "Returns boolean indicating if pattern was found in a column",
|
||||||
result: Some(
|
example: "let df = [[a]; [abc] [acb] [acb]] | polars into-df;
|
||||||
NuDataFrame::try_from_columns(
|
let df2 = $df | polars with-column [(polars col a | polars contains ab | polars as b)] | polars collect;
|
||||||
vec![Column::new(
|
$df2.b",
|
||||||
"0".to_string(),
|
result: Some(
|
||||||
vec![
|
NuDataFrame::try_from_columns(
|
||||||
Value::test_bool(true),
|
vec![Column::new(
|
||||||
Value::test_bool(false),
|
"b".to_string(),
|
||||||
Value::test_bool(false),
|
vec![
|
||||||
],
|
Value::test_bool(true),
|
||||||
)],
|
Value::test_bool(false),
|
||||||
None,
|
Value::test_bool(false),
|
||||||
)
|
],
|
||||||
.expect("simple df for test should not fail")
|
)],
|
||||||
.into_value(Span::test_data()),
|
None,
|
||||||
),
|
)
|
||||||
}]
|
.expect("simple df for test should not fail")
|
||||||
|
.into_value(Span::test_data()),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
Example {
|
||||||
|
description: "Returns boolean indicating if pattern was found",
|
||||||
|
example: "[abc acb acb] | polars into-df | polars contains ab",
|
||||||
|
result: Some(
|
||||||
|
NuDataFrame::try_from_columns(
|
||||||
|
vec![Column::new(
|
||||||
|
"0".to_string(),
|
||||||
|
vec![
|
||||||
|
Value::test_bool(true),
|
||||||
|
Value::test_bool(false),
|
||||||
|
Value::test_bool(false),
|
||||||
|
],
|
||||||
|
)],
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.expect("simple df for test should not fail")
|
||||||
|
.into_value(Span::test_data()),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(
|
fn run(
|
||||||
@ -66,17 +100,48 @@ impl PluginCommand for Contains {
|
|||||||
call: &EvaluatedCall,
|
call: &EvaluatedCall,
|
||||||
input: PipelineData,
|
input: PipelineData,
|
||||||
) -> Result<PipelineData, LabeledError> {
|
) -> Result<PipelineData, LabeledError> {
|
||||||
command(plugin, engine, call, input).map_err(LabeledError::from)
|
let value = input.into_value(call.head)?;
|
||||||
|
match PolarsPluginObject::try_from_value(plugin, &value)? {
|
||||||
|
PolarsPluginObject::NuDataFrame(df) => command_df(plugin, engine, call, df),
|
||||||
|
PolarsPluginObject::NuLazyFrame(lazy) => {
|
||||||
|
let df = lazy.collect(call.head)?;
|
||||||
|
command_df(plugin, engine, call, df)
|
||||||
|
}
|
||||||
|
PolarsPluginObject::NuExpression(expr) => command_expr(plugin, engine, call, expr),
|
||||||
|
_ => Err(cant_convert_err(
|
||||||
|
&value,
|
||||||
|
&[
|
||||||
|
PolarsPluginType::NuDataFrame,
|
||||||
|
PolarsPluginType::NuLazyFrame,
|
||||||
|
PolarsPluginType::NuExpression,
|
||||||
|
],
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
.map_err(LabeledError::from)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn command(
|
fn command_expr(
|
||||||
plugin: &PolarsPlugin,
|
plugin: &PolarsPlugin,
|
||||||
engine: &EngineInterface,
|
engine: &EngineInterface,
|
||||||
call: &EvaluatedCall,
|
call: &EvaluatedCall,
|
||||||
input: PipelineData,
|
expr: NuExpression,
|
||||||
|
) -> Result<PipelineData, ShellError> {
|
||||||
|
let pattern: String = call.req(0)?;
|
||||||
|
let res: NuExpression = expr
|
||||||
|
.into_polars()
|
||||||
|
.str()
|
||||||
|
.contains(lit(pattern), false)
|
||||||
|
.into();
|
||||||
|
res.to_pipeline_data(plugin, engine, call.head)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn command_df(
|
||||||
|
plugin: &PolarsPlugin,
|
||||||
|
engine: &EngineInterface,
|
||||||
|
call: &EvaluatedCall,
|
||||||
|
df: NuDataFrame,
|
||||||
) -> Result<PipelineData, ShellError> {
|
) -> Result<PipelineData, ShellError> {
|
||||||
let df = NuDataFrame::try_from_pipeline_coerce(plugin, input, call.head)?;
|
|
||||||
let pattern: String = call.req(0)?;
|
let pattern: String = call.req(0)?;
|
||||||
|
|
||||||
let series = df.as_series(call.head)?;
|
let series = df.as_series(call.head)?;
|
||||||
|
Loading…
Reference in New Issue
Block a user