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:
Jack Wright 2024-09-04 03:19:29 -07:00 committed by GitHub
parent ebe42241fe
commit eb0de25d19
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 85 additions and 27 deletions

View File

@ -73,6 +73,7 @@ features = [
"strings",
"string_to_integer",
"streaming",
"temporal",
"to_dummies",
]
optional = false

View File

@ -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};
@ -26,15 +31,40 @@ impl PluginCommand for StrFTime {
fn signature(&self) -> Signature {
Signature::build(self.name())
.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()),
)
),
])
.category(Category::Custom("dataframe".into()))
}
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",
example: r#"let dt = ('2020-08-04T16:39:18+00:00' | into datetime --timezone 'UTC');
let df = ([$dt $dt] | polars into-df);
@ -53,7 +83,8 @@ impl PluginCommand for StrFTime {
.expect("simple df for test should not fail")
.into_value(Span::test_data()),
),
}]
},
]
}
fn run(
@ -63,19 +94,45 @@ impl PluginCommand for StrFTime {
call: &EvaluatedCall,
input: PipelineData,
) -> 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,
engine: &EngineInterface,
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> {
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 casted = series.datetime().map_err(|e| ShellError::GenericError {