mirror of
https://github.com/nushell/nushell.git
synced 2025-04-23 20:58:19 +02:00
Expression support for polars strftime
(#13767)
# Description Allows `polars strftime` to be used as an expression: <img width="849" alt="Screenshot 2024-09-03 at 13 14 11" src="https://github.com/user-attachments/assets/2a987fdf-cf00-4c57-b6e1-0b706c594c20"> # User-Facing Changes - `polars strftime` can now be used as an expression.
This commit is contained in:
parent
ebe42241fe
commit
eb0de25d19
@ -73,6 +73,7 @@ features = [
|
|||||||
"strings",
|
"strings",
|
||||||
"string_to_integer",
|
"string_to_integer",
|
||||||
"streaming",
|
"streaming",
|
||||||
|
"temporal",
|
||||||
"to_dummies",
|
"to_dummies",
|
||||||
]
|
]
|
||||||
optional = false
|
optional = false
|
||||||
|
@ -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};
|
||||||
|
|
||||||
@ -26,15 +31,40 @@ impl PluginCommand for StrFTime {
|
|||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
Signature::build(self.name())
|
Signature::build(self.name())
|
||||||
.required("fmt", SyntaxShape::String, "Format rule")
|
.required("fmt", SyntaxShape::String, "Format rule")
|
||||||
.input_output_type(
|
.input_output_types(vec![
|
||||||
|
(
|
||||||
|
Type::Custom("expression".into()),
|
||||||
|
Type::Custom("expression".into()),
|
||||||
|
),
|
||||||
|
(
|
||||||
Type::Custom("dataframe".into()),
|
Type::Custom("dataframe".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![
|
||||||
|
Example {
|
||||||
|
description: "Formats date column as a string",
|
||||||
|
example: r#"let date = '2020-08-04T16:39:18+00:00' | into datetime --timezone 'UTC';
|
||||||
|
let df = ([[a]; [$date]] | polars into-df);
|
||||||
|
let df2 = $df | polars with-column [(polars col a | polars strftime "%Y/%m/%d" | polars as b)] | polars collect;
|
||||||
|
$df2.b"#,
|
||||||
|
result: Some(
|
||||||
|
NuDataFrame::try_from_columns(
|
||||||
|
vec![Column::new(
|
||||||
|
"b".to_string(),
|
||||||
|
vec![Value::test_string("2020/08/04")],
|
||||||
|
)],
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.expect("simple df for test should not fail")
|
||||||
|
.into_value(Span::test_data()),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
Example {
|
||||||
description: "Formats date",
|
description: "Formats date",
|
||||||
example: r#"let dt = ('2020-08-04T16:39:18+00:00' | into datetime --timezone 'UTC');
|
example: r#"let dt = ('2020-08-04T16:39:18+00:00' | into datetime --timezone 'UTC');
|
||||||
let df = ([$dt $dt] | polars into-df);
|
let df = ([$dt $dt] | polars into-df);
|
||||||
@ -53,7 +83,8 @@ impl PluginCommand for StrFTime {
|
|||||||
.expect("simple df for test should not fail")
|
.expect("simple df for test should not fail")
|
||||||
.into_value(Span::test_data()),
|
.into_value(Span::test_data()),
|
||||||
),
|
),
|
||||||
}]
|
},
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(
|
fn run(
|
||||||
@ -63,19 +94,45 @@ impl PluginCommand for StrFTime {
|
|||||||
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 fmt: String = call.req(0)?;
|
||||||
|
let res: NuExpression = expr.into_polars().dt().strftime(&fmt).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 fmt: String = call.req(0)?;
|
let fmt: String = call.req(0)?;
|
||||||
|
|
||||||
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 casted = series.datetime().map_err(|e| ShellError::GenericError {
|
let casted = series.datetime().map_err(|e| ShellError::GenericError {
|
||||||
|
Loading…
Reference in New Issue
Block a user