diff --git a/crates/nu_plugin_polars/src/dataframe/eager/sample.rs b/crates/nu_plugin_polars/src/dataframe/eager/sample.rs index 48ca05959a..59f9ad6eee 100644 --- a/crates/nu_plugin_polars/src/dataframe/eager/sample.rs +++ b/crates/nu_plugin_polars/src/dataframe/eager/sample.rs @@ -1,14 +1,14 @@ use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ - Category, Example, LabeledError, PipelineData, ShellError, Signature, Spanned, SyntaxShape, - Type, + Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, Spanned, + SyntaxShape, Type, Value, }; use polars::prelude::NamedFrom; use polars::series::Series; use crate::{values::CustomValueSupport, PolarsPlugin}; -use super::super::values::NuDataFrame; +use super::super::values::{Column, NuDataFrame}; #[derive(Clone)] pub struct SampleDF; @@ -66,6 +66,23 @@ impl PluginCommand for SampleDF { "[[a b]; [1 2] [3 4] [5 6]] | polars into-df | polars sample --fraction 0.5 --replace", result: None, // No expected value because sampling is random }, + Example { + description: "Shows sample row using using predefined seed 1", + example: + "[[a b]; [1 2] [3 4] [5 6]] | polars into-df | polars sample --seed 1 --n-rows 1", + result: Some( + NuDataFrame::try_from_columns( + vec![ + Column::new("a".to_string(), vec![Value::test_int(5)]), + Column::new("b".to_string(), vec![Value::test_int(6)]), + ], + None, + ) + .expect("should not fail") + .into_value(Span::test_data()), + ) + }, + ] } @@ -133,3 +150,14 @@ fn command( let df = NuDataFrame::new(false, df?); df.to_pipeline_data(plugin, engine, call.head) } + +#[cfg(test)] +mod test { + use super::*; + use crate::test::test_polars_plugin_command; + + #[test] + fn test_examples() -> Result<(), ShellError> { + test_polars_plugin_command(&SampleDF) + } +} diff --git a/crates/nu_plugin_polars/src/dataframe/lazy/to_lazy.rs b/crates/nu_plugin_polars/src/dataframe/lazy/to_lazy.rs index 2437699b54..c4e013ac6a 100644 --- a/crates/nu_plugin_polars/src/dataframe/lazy/to_lazy.rs +++ b/crates/nu_plugin_polars/src/dataframe/lazy/to_lazy.rs @@ -33,10 +33,16 @@ impl PluginCommand for ToLazyFrame { fn examples(&self) -> Vec { vec![Example { - description: "Takes a dictionary and creates a lazy dataframe", + description: "Takes a table and creates a lazyframe", example: "[[a b];[1 2] [3 4]] | polars into-lazy", result: None, - }] + }, + Example { + description: "Takes a table, creates a lazyframe, assigns column 'b' type str, displays the schema", + example: "[[a b];[1 2] [3 4]] | polars into-lazy --schema {b: str} | polars schema", + result: None + }, + ] } fn run( diff --git a/crates/nu_plugin_polars/src/dataframe/series/cumulative.rs b/crates/nu_plugin_polars/src/dataframe/series/cumulative.rs index ad4740663e..7455297b59 100644 --- a/crates/nu_plugin_polars/src/dataframe/series/cumulative.rs +++ b/crates/nu_plugin_polars/src/dataframe/series/cumulative.rs @@ -67,27 +67,50 @@ impl PluginCommand for Cumulative { } fn examples(&self) -> Vec { - vec![Example { - description: "Cumulative sum for a series", - example: "[1 2 3 4 5] | polars into-df | polars cumulative sum", - result: Some( - NuDataFrame::try_from_columns( - vec![Column::new( - "0_cumulative_sum".to_string(), - vec![ - Value::test_int(1), - Value::test_int(3), - Value::test_int(6), - Value::test_int(10), - Value::test_int(15), - ], - )], - None, - ) - .expect("simple df for test should not fail") - .into_value(Span::test_data()), - ), - }] + vec![ + Example { + description: "Cumulative sum for a series", + example: "[1 2 3 4 5] | polars into-df | polars cumulative sum", + result: Some( + NuDataFrame::try_from_columns( + vec![Column::new( + "0_cumulative_sum".to_string(), + vec![ + Value::test_int(1), + Value::test_int(3), + Value::test_int(6), + Value::test_int(10), + Value::test_int(15), + ], + )], + None, + ) + .expect("simple df for test should not fail") + .into_value(Span::test_data()), + ), + }, + Example { + description: "Cumulative sum for a series in reverse order", + example: "[1 2 3 4 5] | polars into-df | polars cumulative sum --reverse", + result: Some( + NuDataFrame::try_from_columns( + vec![Column::new( + "0_cumulative_sum".to_string(), + vec![ + Value::test_int(15), + Value::test_int(14), + Value::test_int(12), + Value::test_int(9), + Value::test_int(5), + ], + )], + None, + ) + .expect("simple df for test should not fail") + .into_value(Span::test_data()), + ), + }, + ] } fn run( diff --git a/crates/nu_plugin_polars/src/dataframe/series/date/as_date.rs b/crates/nu_plugin_polars/src/dataframe/series/date/as_date.rs index 5228a25e51..414853d291 100644 --- a/crates/nu_plugin_polars/src/dataframe/series/date/as_date.rs +++ b/crates/nu_plugin_polars/src/dataframe/series/date/as_date.rs @@ -41,11 +41,18 @@ impl PluginCommand for AsDate { } fn examples(&self) -> Vec { - vec![Example { - description: "Converts string to date", - example: r#"["2021-12-30" "2021-12-31"] | polars into-df | polars as-date "%Y-%m-%d""#, - result: None, - }] + vec![ + Example { + description: "Converts string to date", + example: r#"["2021-12-30" "2021-12-31"] | polars into-df | polars as-date "%Y-%m-%d""#, + result: None, // help is needed on how to provide results + }, + Example { + description: "Converts string to date", + example: r#"["2021-12-30" "2021-12-31 21:00:00"] | polars into-df | polars as-date "%Y-%m-%d" --not-exact"#, + result: None, + }, + ] } fn run( @@ -99,3 +106,14 @@ fn command( let df = NuDataFrame::try_from_series_vec(vec![res], call.head)?; df.to_pipeline_data(plugin, engine, call.head) } + +#[cfg(test)] +mod test { + use super::*; + use crate::test::test_polars_plugin_command; + + #[test] + fn test_examples() -> Result<(), ShellError> { + test_polars_plugin_command(&AsDate) + } +} diff --git a/crates/nu_plugin_polars/src/dataframe/series/date/as_datetime.rs b/crates/nu_plugin_polars/src/dataframe/series/date/as_datetime.rs index 2cbea57507..8e01fda6ac 100644 --- a/crates/nu_plugin_polars/src/dataframe/series/date/as_datetime.rs +++ b/crates/nu_plugin_polars/src/dataframe/series/date/as_datetime.rs @@ -116,6 +116,30 @@ impl PluginCommand for AsDateTime { .into_value(Span::test_data()), ), }, + Example { + description: "Converts string to datetime using the `--not-exact` flag even with excessive symbols", + example: r#"["2021-12-30 00:00:00 GMT+4"] | polars into-df | polars as-datetime "%Y-%m-%d %H:%M:%S" --not-exact"#, + result: Some( + NuDataFrame::try_from_columns( + vec![Column::new( + "datetime".to_string(), + vec![ + Value::date( + DateTime::parse_from_str( + "2021-12-30 00:00:00 +0000", + "%Y-%m-%d %H:%M:%S %z", + ) + .expect("date calculation should not fail in test"), + Span::test_data(), + ), + ], + )], + None, + ) + .expect("simple df for test should not fail") + .into_value(Span::test_data()), + ), + }, ] } diff --git a/crates/nu_plugin_polars/src/dataframe/series/shift.rs b/crates/nu_plugin_polars/src/dataframe/series/shift.rs index 9b5f8b2f63..64e8fe5b62 100644 --- a/crates/nu_plugin_polars/src/dataframe/series/shift.rs +++ b/crates/nu_plugin_polars/src/dataframe/series/shift.rs @@ -45,21 +45,45 @@ impl PluginCommand for Shift { } fn examples(&self) -> Vec { - vec![Example { - description: "Shifts the values by a given period", - example: "[1 2 2 3 3] | polars into-df | polars shift 2 | polars drop-nulls", - result: Some( - NuDataFrame::try_from_columns( - vec![Column::new( - "0".to_string(), - vec![Value::test_int(1), Value::test_int(2), Value::test_int(2)], - )], - None, - ) - .expect("simple df for test should not fail") - .into_value(Span::test_data()), - ), - }] + vec![ + Example { + description: "Shifts the values by a given period", + example: "[1 2 2 3 3] | polars into-df | polars shift 2 | polars drop-nulls", + result: Some( + NuDataFrame::try_from_columns( + vec![Column::new( + "0".to_string(), + vec![Value::test_int(1), Value::test_int(2), Value::test_int(2)], + )], + None, + ) + .expect("simple df for test should not fail") + .into_value(Span::test_data()), + ), + }, + Example { + description: "Shifts the values by a given period, fill absent values with 0", + example: + "[1 2 2 3 3] | polars into-lazy | polars shift 2 --fill 0 | polars collect", + result: Some( + NuDataFrame::try_from_columns( + vec![Column::new( + "0".to_string(), + vec![ + Value::test_int(0), + Value::test_int(0), + Value::test_int(1), + Value::test_int(2), + Value::test_int(2), + ], + )], + None, + ) + .expect("simple df for test should not fail") + .into_value(Span::test_data()), + ), + }, + ] } fn run(