mirror of
https://github.com/nushell/nushell.git
synced 2025-05-08 12:04:25 +02:00
Added expression support for polars str-lengths
(#13782)
# Description Allows `polars str-lengths` to be used as an expression: <img width="826" alt="Screenshot 2024-09-04 at 13 57 45" src="https://github.com/user-attachments/assets/b74139e0-e8ba-4910-84c2-cf4be4a084b6"> # User-Facing Changes - `polars str-lengths` can be used as an expression. - char length is now the default. Use the --bytes flag to get bytes length.
This commit is contained in:
parent
f611196373
commit
e7c5f83460
@ -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};
|
||||||
|
|
||||||
@ -24,29 +29,57 @@ impl PluginCommand for StrLengths {
|
|||||||
|
|
||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
Signature::build(self.name())
|
Signature::build(self.name())
|
||||||
.input_output_type(
|
.switch(
|
||||||
Type::Custom("dataframe".into()),
|
"bytes",
|
||||||
Type::Custom("dataframe".into()),
|
"Get the length in bytes instead of chars.",
|
||||||
|
Some('b'),
|
||||||
)
|
)
|
||||||
|
.input_output_types(vec![
|
||||||
|
(
|
||||||
|
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 string lengths",
|
Example {
|
||||||
example: "[a ab abc] | polars into-df | polars str-lengths",
|
description: "Returns string lengths for a column",
|
||||||
result: Some(
|
example: "[[a]; [a] [ab] [abc]] | polars into-df | polars select (polars col a | polars str-lengths) | polars collect",
|
||||||
NuDataFrame::try_from_columns(
|
result: Some(
|
||||||
vec![Column::new(
|
NuDataFrame::try_from_columns(
|
||||||
"0".to_string(),
|
vec![Column::new(
|
||||||
vec![Value::test_int(1), Value::test_int(2), Value::test_int(3)],
|
"a".to_string(),
|
||||||
)],
|
vec![Value::test_int(1), Value::test_int(2), Value::test_int(3)],
|
||||||
None,
|
)],
|
||||||
)
|
None,
|
||||||
.expect("simple df for test should not fail")
|
)
|
||||||
.into_value(Span::test_data()),
|
.expect("simple df for test should not fail")
|
||||||
),
|
.into_value(Span::test_data()),
|
||||||
}]
|
),
|
||||||
|
},
|
||||||
|
Example {
|
||||||
|
description: "Returns string lengths",
|
||||||
|
example: "[a ab abc] | polars into-df | polars str-lengths",
|
||||||
|
result: Some(
|
||||||
|
NuDataFrame::try_from_columns(
|
||||||
|
vec![Column::new(
|
||||||
|
"0".to_string(),
|
||||||
|
vec![Value::test_int(1), Value::test_int(2), Value::test_int(3)],
|
||||||
|
)],
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.expect("simple df for test should not fail")
|
||||||
|
.into_value(Span::test_data()),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(
|
fn run(
|
||||||
@ -56,17 +89,46 @@ impl PluginCommand for StrLengths {
|
|||||||
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) => {
|
||||||
|
command_df(plugin, engine, call, lazy.collect(call.head)?)
|
||||||
|
}
|
||||||
|
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 res: NuExpression = if call.has_flag("bytes")? {
|
||||||
|
expr.into_polars().str().len_bytes().into()
|
||||||
|
} else {
|
||||||
|
expr.into_polars().str().len_chars().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 series = df.as_series(call.head)?;
|
let series = df.as_series(call.head)?;
|
||||||
|
|
||||||
let chunked = series.str().map_err(|e| ShellError::GenericError {
|
let chunked = series.str().map_err(|e| ShellError::GenericError {
|
||||||
@ -77,7 +139,11 @@ fn command(
|
|||||||
inner: vec![],
|
inner: vec![],
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let res = chunked.as_ref().str_len_bytes().into_series();
|
let res = if call.has_flag("bytes")? {
|
||||||
|
chunked.as_ref().str_len_bytes().into_series()
|
||||||
|
} else {
|
||||||
|
chunked.as_ref().str_len_chars().into_series()
|
||||||
|
};
|
||||||
|
|
||||||
let df = NuDataFrame::try_from_series_vec(vec![res], call.head)?;
|
let df = NuDataFrame::try_from_series_vec(vec![res], call.head)?;
|
||||||
df.to_pipeline_data(plugin, engine, call.head)
|
df.to_pipeline_data(plugin, engine, call.head)
|
||||||
|
Loading…
Reference in New Issue
Block a user