From 17008bb6484bb4a76a14801bea07b733888819ce Mon Sep 17 00:00:00 2001 From: Fernando Herrera Date: Thu, 1 Jul 2021 05:33:52 +0100 Subject: [PATCH] Removed list from dataframe command signatures (#3713) * Type in command description * filter name change * Clean column name * Clippy error and updated polars version * Lint correction in file * CSV Infer schema optional * Correct float operations * changes in series castings to allow other types * Clippy error correction * Removed lists from command signatures * Added not command for series --- .../src/commands/dataframe/aggregate.rs | 37 +------------ .../nu-command/src/commands/dataframe/drop.rs | 10 +--- .../src/commands/dataframe/drop_nulls.rs | 8 +-- .../src/commands/dataframe/filter.rs | 4 +- .../commands/dataframe/{head.rs => first.rs} | 6 +- .../nu-command/src/commands/dataframe/get.rs | 10 +--- .../src/commands/dataframe/groupby.rs | 10 +--- .../nu-command/src/commands/dataframe/join.rs | 21 ++++--- .../commands/dataframe/{tail.rs => last.rs} | 8 +-- .../nu-command/src/commands/dataframe/melt.rs | 10 +--- .../nu-command/src/commands/dataframe/mod.rs | 13 +++-- .../commands/dataframe/{load.rs => open.rs} | 8 +-- .../src/commands/dataframe/pivot.rs | 2 +- .../src/commands/dataframe/sample.rs | 2 +- .../src/commands/dataframe/select.rs | 10 +--- .../commands/dataframe/series/all_false.rs | 4 +- .../src/commands/dataframe/series/all_true.rs | 4 +- .../src/commands/dataframe/series/is_in.rs | 2 +- .../commands/dataframe/series/is_not_null.rs | 4 +- .../src/commands/dataframe/series/is_null.rs | 4 +- .../src/commands/dataframe/series/mod.rs | 2 + .../src/commands/dataframe/series/n_null.rs | 2 +- .../src/commands/dataframe/series/not.rs | 55 +++++++++++++++++++ .../src/commands/dataframe/series/set.rs | 4 +- .../nu-command/src/commands/dataframe/sort.rs | 31 +++++------ crates/nu-command/src/commands/mod.rs | 12 ++-- crates/nu-command/src/default_context.rs | 7 ++- 27 files changed, 148 insertions(+), 142 deletions(-) rename crates/nu-command/src/commands/dataframe/{head.rs => first.rs} (92%) rename crates/nu-command/src/commands/dataframe/{tail.rs => last.rs} (88%) rename crates/nu-command/src/commands/dataframe/{load.rs => open.rs} (97%) create mode 100644 crates/nu-command/src/commands/dataframe/series/not.rs diff --git a/crates/nu-command/src/commands/dataframe/aggregate.rs b/crates/nu-command/src/commands/dataframe/aggregate.rs index be411e0544..cfb1cf6215 100644 --- a/crates/nu-command/src/commands/dataframe/aggregate.rs +++ b/crates/nu-command/src/commands/dataframe/aggregate.rs @@ -11,8 +11,6 @@ use polars::{ prelude::{DataType, PolarsError, Series}, }; -use super::utils::convert_columns; - enum Operation { Mean, Sum, @@ -90,11 +88,6 @@ impl WholeStreamCommand for DataFrame { fn signature(&self) -> Signature { Signature::build("dataframe aggregate") .required("operation", SyntaxShape::String, "aggregate operation") - .optional( - "selection", - SyntaxShape::Table, - "columns to perform aggregation", - ) .named( "quantile", SyntaxShape::Number, @@ -117,7 +110,7 @@ impl WholeStreamCommand for DataFrame { Example { description: "Aggregate sum by grouping by column a and summing on col b", example: - "[[a b]; [one 1] [one 2]] | dataframe to-df | dataframe group-by [a] | dataframe aggregate sum", + "[[a b]; [one 1] [one 2]] | dataframe to-df | dataframe group-by a | dataframe aggregate sum", result: None, }, Example { @@ -141,16 +134,6 @@ fn command(mut args: CommandArgs) -> Result { let operation: Tagged = args.req(0)?; let op = Operation::from_tagged(&operation, quantile)?; - // Extracting the selection columns of the columns to perform the aggregation - let agg_cols: Option> = args.opt(1)?; - let (selection, agg_span) = match agg_cols { - Some(cols) => { - let (agg_string, agg_span) = convert_columns(&cols, &tag)?; - (Some(agg_string), agg_span) - } - None => (None, Span::unknown()), - }; - let value = args.input.next().ok_or_else(|| { ShellError::labeled_error("Empty stream", "No value found in the stream", &tag) })?; @@ -159,16 +142,11 @@ fn command(mut args: CommandArgs) -> Result { UntaggedValue::DataFrame(PolarsData::GroupBy(nu_groupby)) => { let groupby = nu_groupby.to_groupby()?; - let groupby = match &selection { - Some(cols) => groupby.select(cols), - None => groupby, - }; - let res = perform_groupby_aggregation( groupby, op, &operation.tag, - &agg_span, + &tag.span, args.has_flag("explicit"), )?; @@ -177,16 +155,7 @@ fn command(mut args: CommandArgs) -> Result { UntaggedValue::DataFrame(PolarsData::EagerDataFrame(df)) => { let df = df.as_ref(); - let res = match &selection { - Some(cols) => { - let df = df - .select(cols) - .map_err(|e| parse_polars_error::<&str>(&e, &agg_span, None))?; - - perform_dataframe_aggregation(&df, op, &operation.tag) - } - None => perform_dataframe_aggregation(&df, op, &operation.tag), - }?; + let res = perform_dataframe_aggregation(&df, op, &operation.tag)?; Ok(OutputStream::one(NuDataFrame::dataframe_to_value(res, tag))) } diff --git a/crates/nu-command/src/commands/dataframe/drop.rs b/crates/nu-command/src/commands/dataframe/drop.rs index fe40991e53..387cf7cee5 100644 --- a/crates/nu-command/src/commands/dataframe/drop.rs +++ b/crates/nu-command/src/commands/dataframe/drop.rs @@ -17,11 +17,7 @@ impl WholeStreamCommand for DataFrame { } fn signature(&self) -> Signature { - Signature::build("dataframe drop").required( - "columns", - SyntaxShape::Table, - "column names to be dropped", - ) + Signature::build("dataframe drop").rest(SyntaxShape::Any, "column names to be dropped") } fn run(&self, args: CommandArgs) -> Result { @@ -31,7 +27,7 @@ impl WholeStreamCommand for DataFrame { fn examples(&self) -> Vec { vec![Example { description: "drop column a", - example: "[[a b]; [1 2] [3 4]] | dataframe to-df | dataframe drop [a]", + example: "[[a b]; [1 2] [3 4]] | dataframe to-df | dataframe drop a", result: None, }] } @@ -40,7 +36,7 @@ impl WholeStreamCommand for DataFrame { fn command(mut args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); - let columns: Vec = args.req(0)?; + let columns: Vec = args.rest(0)?; let (col_string, col_span) = convert_columns(&columns, &tag)?; let df = NuDataFrame::try_from_stream(&mut args.input, &tag.span)?; diff --git a/crates/nu-command/src/commands/dataframe/drop_nulls.rs b/crates/nu-command/src/commands/dataframe/drop_nulls.rs index 5e51632902..72659962ec 100644 --- a/crates/nu-command/src/commands/dataframe/drop_nulls.rs +++ b/crates/nu-command/src/commands/dataframe/drop_nulls.rs @@ -36,16 +36,16 @@ impl WholeStreamCommand for DataFrame { Example { description: "drop null values in dataframe", example: r#"let df = ([[a b]; [1 2] [3 0] [1 2]] | dataframe to-df); -let res = ($df.b / $df.b); -let df = ($df | dataframe with-column $res --name res); -$df | dataframe drop-nulls + let res = ($df.b / $df.b); + let df = ($df | dataframe with-column $res --name res); + $df | dataframe drop-nulls "#, result: None, }, Example { description: "drop null values in dataframe", example: r#"let s = ([1 2 0 0 3 4] | dataframe to-series); -($s / $s) | dataframe drop-nulls"#, + ($s / $s) | dataframe drop-nulls"#, result: None, }, ] diff --git a/crates/nu-command/src/commands/dataframe/filter.rs b/crates/nu-command/src/commands/dataframe/filter.rs index 615f5e94ac..07e4848008 100644 --- a/crates/nu-command/src/commands/dataframe/filter.rs +++ b/crates/nu-command/src/commands/dataframe/filter.rs @@ -35,13 +35,13 @@ impl WholeStreamCommand for DataFrame { Example { description: "Filter dataframe using a bool mask", example: r#"let mask = ([$true $false] | dataframe to-series); -[[a b]; [1 2] [3 4]] | dataframe to-df | dataframe filter-with $mask"#, + [[a b]; [1 2] [3 4]] | dataframe to-df | dataframe filter-with $mask"#, result: None, }, Example { description: "Filter dataframe by creating a mask from operation", example: r#"let mask = (([5 6] | dataframe to-series) > 5); -[[a b]; [1 2] [3 4]] | dataframe to-df | dataframe filter-with $mask"#, + [[a b]; [1 2] [3 4]] | dataframe to-df | dataframe filter-with $mask"#, result: None, }, ] diff --git a/crates/nu-command/src/commands/dataframe/head.rs b/crates/nu-command/src/commands/dataframe/first.rs similarity index 92% rename from crates/nu-command/src/commands/dataframe/head.rs rename to crates/nu-command/src/commands/dataframe/first.rs index 615b138828..aae838266f 100644 --- a/crates/nu-command/src/commands/dataframe/head.rs +++ b/crates/nu-command/src/commands/dataframe/first.rs @@ -9,11 +9,11 @@ pub struct DataFrame; impl WholeStreamCommand for DataFrame { fn name(&self) -> &str { - "dataframe head" + "dataframe first" } fn usage(&self) -> &str { - "[DataFrame] Creates new dataframe with head rows" + "[DataFrame] Creates new dataframe with first rows" } fn signature(&self) -> Signature { @@ -31,7 +31,7 @@ impl WholeStreamCommand for DataFrame { fn examples(&self) -> Vec { vec![Example { description: "Create new dataframe with head rows", - example: "[[a b]; [1 2] [3 4]] | dataframe to-df | dataframe head", + example: "[[a b]; [1 2] [3 4]] | dataframe to-df | dataframe first", result: None, }] } diff --git a/crates/nu-command/src/commands/dataframe/get.rs b/crates/nu-command/src/commands/dataframe/get.rs index 0b70741fa4..6d015615f6 100644 --- a/crates/nu-command/src/commands/dataframe/get.rs +++ b/crates/nu-command/src/commands/dataframe/get.rs @@ -16,11 +16,7 @@ impl WholeStreamCommand for DataFrame { } fn signature(&self) -> Signature { - Signature::build("dataframe get").required( - "columns", - SyntaxShape::Table, - "column names to sort dataframe", - ) + Signature::build("dataframe get").rest(SyntaxShape::Any, "column names to sort dataframe") } fn run(&self, args: CommandArgs) -> Result { @@ -30,7 +26,7 @@ impl WholeStreamCommand for DataFrame { fn examples(&self) -> Vec { vec![Example { description: "Creates dataframe with selected columns", - example: "[[a b]; [1 2] [3 4]] | dataframe to-df | dataframe get [a]", + example: "[[a b]; [1 2] [3 4]] | dataframe to-df | dataframe get a", result: None, }] } @@ -38,7 +34,7 @@ impl WholeStreamCommand for DataFrame { fn command(mut args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); - let columns: Vec = args.req(0)?; + let columns: Vec = args.rest(0)?; let (col_string, col_span) = convert_columns(&columns, &tag)?; diff --git a/crates/nu-command/src/commands/dataframe/groupby.rs b/crates/nu-command/src/commands/dataframe/groupby.rs index 3747e308a8..8844d3a4d7 100644 --- a/crates/nu-command/src/commands/dataframe/groupby.rs +++ b/crates/nu-command/src/commands/dataframe/groupby.rs @@ -20,11 +20,7 @@ impl WholeStreamCommand for DataFrame { } fn signature(&self) -> Signature { - Signature::build("dataframe group-by").required( - "by columns", - SyntaxShape::Table, - "groupby columns", - ) + Signature::build("dataframe group-by").rest(SyntaxShape::Any, "groupby columns") } fn run(&self, args: CommandArgs) -> Result { @@ -34,7 +30,7 @@ impl WholeStreamCommand for DataFrame { fn examples(&self) -> Vec { vec![Example { description: "Grouping by column a", - example: "[[a b]; [one 1] [one 2]] | dataframe to-df | dataframe group-by [a]", + example: "[[a b]; [one 1] [one 2]] | dataframe to-df | dataframe group-by a", result: None, }] } @@ -44,7 +40,7 @@ fn command(mut args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); // Extracting the names of the columns to perform the groupby - let by_columns: Vec = args.req(0)?; + let by_columns: Vec = args.rest(0)?; let (columns_string, col_span) = convert_columns(&by_columns, &tag)?; let df = NuDataFrame::try_from_stream(&mut args.input, &tag.span)?; diff --git a/crates/nu-command/src/commands/dataframe/join.rs b/crates/nu-command/src/commands/dataframe/join.rs index 8decf80b20..757de35ddc 100644 --- a/crates/nu-command/src/commands/dataframe/join.rs +++ b/crates/nu-command/src/commands/dataframe/join.rs @@ -26,15 +26,17 @@ impl WholeStreamCommand for DataFrame { fn signature(&self) -> Signature { Signature::build("dataframe join") .required("dataframe", SyntaxShape::Any, "right dataframe to join") - .required( - "l_columns", + .required_named( + "left", SyntaxShape::Table, "left column names to perform join", + Some('l'), ) - .required( - "r_columns", + .required_named( + "right", SyntaxShape::Table, "right column names to perform join", + Some('r'), ) .named( "type", @@ -52,13 +54,14 @@ impl WholeStreamCommand for DataFrame { vec![ Example { description: "inner join dataframe", - example: "echo [[a b]; [1 2] [3 4]] | dataframe to-df | dataframe join $right [a] [a]", + example: r#"let right = ([[a b c]; [1 2 5] [3 4 5] [5 6 6]] | dataframe to-df); + $right | dataframe join $right -l [a b] -r [a b]"#, result: None, }, Example { description: "right join dataframe", - example: - "[[a b]; [1 2] [3 4] [5 6]] | dataframe to-df | dataframe join $right [b] [b] -t right", + example: r#"let right = ([[a b c]; [1 2 3] [3 4 5] [5 6 7]] | dataframe to-df); + $right | dataframe join $right -l [a c] -r [a c] -t inner"#, result: None, }, ] @@ -69,8 +72,8 @@ fn command(mut args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); let r_df: Value = args.req(0)?; - let l_col: Vec = args.req(1)?; - let r_col: Vec = args.req(2)?; + let l_col: Vec = args.req_named("left")?; + let r_col: Vec = args.req_named("right")?; let join_type_op: Option> = args.get_flag("type")?; let join_type = match join_type_op { diff --git a/crates/nu-command/src/commands/dataframe/tail.rs b/crates/nu-command/src/commands/dataframe/last.rs similarity index 88% rename from crates/nu-command/src/commands/dataframe/tail.rs rename to crates/nu-command/src/commands/dataframe/last.rs index 2eb3612649..b34dae0635 100644 --- a/crates/nu-command/src/commands/dataframe/tail.rs +++ b/crates/nu-command/src/commands/dataframe/last.rs @@ -8,7 +8,7 @@ pub struct DataFrame; impl WholeStreamCommand for DataFrame { fn name(&self) -> &str { - "dataframe tail" + "dataframe last" } fn usage(&self) -> &str { @@ -16,7 +16,7 @@ impl WholeStreamCommand for DataFrame { } fn signature(&self) -> Signature { - Signature::build("dataframe tail").optional( + Signature::build("dataframe last").optional( "n_rows", SyntaxShape::Number, "Number of rows for tail", @@ -29,8 +29,8 @@ impl WholeStreamCommand for DataFrame { fn examples(&self) -> Vec { vec![Example { - description: "Create new dataframe with tail rows", - example: "[[a b]; [1 2] [3 4]] | dataframe to-df | dataframe tail", + description: "Create new dataframe with last rows", + example: "[[a b]; [1 2] [3 4]] | dataframe to-df | dataframe last", result: None, }] } diff --git a/crates/nu-command/src/commands/dataframe/melt.rs b/crates/nu-command/src/commands/dataframe/melt.rs index 7df3f34edf..678af7ba2b 100644 --- a/crates/nu-command/src/commands/dataframe/melt.rs +++ b/crates/nu-command/src/commands/dataframe/melt.rs @@ -19,11 +19,7 @@ impl WholeStreamCommand for DataFrame { fn signature(&self) -> Signature { Signature::build("dataframe melt") .required("id_columns", SyntaxShape::Table, "Id columns for melting") - .required( - "value_columns", - SyntaxShape::Table, - "columns used as value columns", - ) + .rest(SyntaxShape::Any, "columns used as value columns") } fn run(&self, args: CommandArgs) -> Result { @@ -33,7 +29,7 @@ impl WholeStreamCommand for DataFrame { fn examples(&self) -> Vec { vec![Example { description: "melt dataframe", - example: "[[a b]; [a 2] [b 4] [a 6]] | dataframe to-df | dataframe melt [a] [b]", + example: "[[a b]; [a 2] [b 4] [a 6]] | dataframe to-df | dataframe melt a b", result: None, }] } @@ -43,7 +39,7 @@ fn command(mut args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); let id_col: Vec = args.req(0)?; - let val_col: Vec = args.req(1)?; + let val_col: Vec = args.rest(1)?; let (id_col_string, id_col_span) = convert_columns(&id_col, &tag)?; let (val_col_string, val_col_span) = convert_columns(&val_col, &tag)?; diff --git a/crates/nu-command/src/commands/dataframe/mod.rs b/crates/nu-command/src/commands/dataframe/mod.rs index 337b178473..bb33915548 100644 --- a/crates/nu-command/src/commands/dataframe/mod.rs +++ b/crates/nu-command/src/commands/dataframe/mod.rs @@ -7,20 +7,20 @@ pub mod drop_nulls; pub mod dtypes; pub mod dummies; pub mod filter; +pub mod first; pub mod get; pub mod groupby; -pub mod head; pub mod join; +pub mod last; pub mod list; -pub mod load; pub mod melt; +pub mod open; pub mod pivot; pub mod sample; pub mod select; pub mod show; pub mod slice; pub mod sort; -pub mod tail; pub mod to_csv; pub mod to_df; pub mod to_parquet; @@ -38,20 +38,20 @@ pub use drop_nulls::DataFrame as DataFrameDropNulls; pub use dtypes::DataFrame as DataFrameDTypes; pub use dummies::DataFrame as DataFrameDummies; pub use filter::DataFrame as DataFrameFilter; +pub use first::DataFrame as DataFrameFirst; pub use get::DataFrame as DataFrameGet; pub use groupby::DataFrame as DataFrameGroupBy; -pub use head::DataFrame as DataFrameHead; pub use join::DataFrame as DataFrameJoin; +pub use last::DataFrame as DataFrameLast; pub use list::DataFrame as DataFrameList; -pub use load::DataFrame as DataFrameLoad; pub use melt::DataFrame as DataFrameMelt; +pub use open::DataFrame as DataFrameOpen; pub use pivot::DataFrame as DataFramePivot; pub use sample::DataFrame as DataFrameSample; pub use select::DataFrame as DataFrameSelect; pub use show::DataFrame as DataFrameShow; pub use slice::DataFrame as DataFrameSlice; pub use sort::DataFrame as DataFrameSort; -pub use tail::DataFrame as DataFrameTail; pub use to_csv::DataFrame as DataFrameToCsv; pub use to_df::DataFrame as DataFrameToDF; pub use to_parquet::DataFrame as DataFrameToParquet; @@ -74,6 +74,7 @@ pub use series::DataFrameIsNull; pub use series::DataFrameIsUnique; pub use series::DataFrameNNull; pub use series::DataFrameNUnique; +pub use series::DataFrameNot; pub use series::DataFrameSeriesRename; pub use series::DataFrameSet; pub use series::DataFrameShift; diff --git a/crates/nu-command/src/commands/dataframe/load.rs b/crates/nu-command/src/commands/dataframe/open.rs similarity index 97% rename from crates/nu-command/src/commands/dataframe/load.rs rename to crates/nu-command/src/commands/dataframe/open.rs index edea46d432..bc644aa31e 100644 --- a/crates/nu-command/src/commands/dataframe/load.rs +++ b/crates/nu-command/src/commands/dataframe/open.rs @@ -15,15 +15,15 @@ pub struct DataFrame; impl WholeStreamCommand for DataFrame { fn name(&self) -> &str { - "dataframe load" + "dataframe open" } fn usage(&self) -> &str { - "Loads dataframe form csv file" + "Opens csv, json or parquet file to create dataframe" } fn signature(&self) -> Signature { - Signature::build("dataframe load") + Signature::build("dataframe open") .required( "file", SyntaxShape::FilePath, @@ -67,7 +67,7 @@ impl WholeStreamCommand for DataFrame { fn examples(&self) -> Vec { vec![Example { description: "Takes a file name and creates a dataframe", - example: "dataframe load test.csv", + example: "dataframe open test.csv", result: None, }] } diff --git a/crates/nu-command/src/commands/dataframe/pivot.rs b/crates/nu-command/src/commands/dataframe/pivot.rs index 206fa1d58f..088bcc9a2f 100644 --- a/crates/nu-command/src/commands/dataframe/pivot.rs +++ b/crates/nu-command/src/commands/dataframe/pivot.rs @@ -72,7 +72,7 @@ impl WholeStreamCommand for DataFrame { vec![Example { description: "Pivot a dataframe on b and aggregation on col c", example: - "[[a b c]; [one x 1] [two y 2]] | dataframe to-df | dataframe group-by [a] | dataframe pivot b c sum", + "[[a b c]; [one x 1] [two y 2]] | dataframe to-df | dataframe group-by a | dataframe pivot b c sum", result: None, }] } diff --git a/crates/nu-command/src/commands/dataframe/sample.rs b/crates/nu-command/src/commands/dataframe/sample.rs index 9776eded6b..b59d8afec8 100644 --- a/crates/nu-command/src/commands/dataframe/sample.rs +++ b/crates/nu-command/src/commands/dataframe/sample.rs @@ -17,7 +17,7 @@ impl WholeStreamCommand for DataFrame { } fn signature(&self) -> Signature { - Signature::build("dataframe load") + Signature::build("dataframe sample") .named( "n_rows", SyntaxShape::Number, diff --git a/crates/nu-command/src/commands/dataframe/select.rs b/crates/nu-command/src/commands/dataframe/select.rs index 3b84395d79..ea86ab8229 100644 --- a/crates/nu-command/src/commands/dataframe/select.rs +++ b/crates/nu-command/src/commands/dataframe/select.rs @@ -17,11 +17,7 @@ impl WholeStreamCommand for DataFrame { } fn signature(&self) -> Signature { - Signature::build("dataframe select").required( - "columns", - SyntaxShape::Table, - "selected column names", - ) + Signature::build("dataframe select").rest(SyntaxShape::Any, "selected column names") } fn run(&self, args: CommandArgs) -> Result { @@ -31,7 +27,7 @@ impl WholeStreamCommand for DataFrame { fn examples(&self) -> Vec { vec![Example { description: "Create new dataframe with column a", - example: "[[a b]; [1 2] [3 4]] | dataframe to-df | dataframe select [a]", + example: "[[a b]; [1 2] [3 4]] | dataframe to-df | dataframe select a", result: None, }] } @@ -40,7 +36,7 @@ impl WholeStreamCommand for DataFrame { fn command(mut args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); - let columns: Vec = args.req(0)?; + let columns: Vec = args.rest(0)?; let (col_string, col_span) = convert_columns(&columns, &tag)?; diff --git a/crates/nu-command/src/commands/dataframe/series/all_false.rs b/crates/nu-command/src/commands/dataframe/series/all_false.rs index 110430991b..8d54416670 100644 --- a/crates/nu-command/src/commands/dataframe/series/all_false.rs +++ b/crates/nu-command/src/commands/dataframe/series/all_false.rs @@ -32,8 +32,8 @@ impl WholeStreamCommand for DataFrame { Example { description: "Checks the result from a comparison", example: r#"let s = ([5 6 2 8] | dataframe to-series); -let res = ($s > 9); -$res | dataframe all-false"#, + let res = ($s > 9); + $res | dataframe all-false"#, result: None, }, ] diff --git a/crates/nu-command/src/commands/dataframe/series/all_true.rs b/crates/nu-command/src/commands/dataframe/series/all_true.rs index 38f82656b6..85e6d5ad00 100644 --- a/crates/nu-command/src/commands/dataframe/series/all_true.rs +++ b/crates/nu-command/src/commands/dataframe/series/all_true.rs @@ -32,8 +32,8 @@ impl WholeStreamCommand for DataFrame { Example { description: "Checks the result from a comparison", example: r#"let s = ([5 6 2 8] | dataframe to-series); -let res = ($s > 9); -$res | dataframe all-true"#, + let res = ($s > 9); + $res | dataframe all-true"#, result: None, }, ] diff --git a/crates/nu-command/src/commands/dataframe/series/is_in.rs b/crates/nu-command/src/commands/dataframe/series/is_in.rs index 8a335d1a97..21452b1b12 100644 --- a/crates/nu-command/src/commands/dataframe/series/is_in.rs +++ b/crates/nu-command/src/commands/dataframe/series/is_in.rs @@ -30,7 +30,7 @@ impl WholeStreamCommand for DataFrame { vec![Example { description: "Checks if elements from a series are contained in right series", example: r#"let other = ([1 3 6] | dataframe to-series); -[5 6 6 6 8 8 8] | dataframe to-series | dataframe is-in $other"#, + [5 6 6 6 8 8 8] | dataframe to-series | dataframe is-in $other"#, result: None, }] } diff --git a/crates/nu-command/src/commands/dataframe/series/is_not_null.rs b/crates/nu-command/src/commands/dataframe/series/is_not_null.rs index 00da975c6f..ad3d4cb77c 100644 --- a/crates/nu-command/src/commands/dataframe/series/is_not_null.rs +++ b/crates/nu-command/src/commands/dataframe/series/is_not_null.rs @@ -27,8 +27,8 @@ impl WholeStreamCommand for DataFrame { vec![Example { description: "Create mask where values are not null", example: r#"let s = ([5 6 0 8] | dataframe to-series); -let res = ($s / $s); -$res | dataframe is-not-null"#, + let res = ($s / $s); + $res | dataframe is-not-null"#, result: None, }] } diff --git a/crates/nu-command/src/commands/dataframe/series/is_null.rs b/crates/nu-command/src/commands/dataframe/series/is_null.rs index 620d31e3f5..20d41d8b95 100644 --- a/crates/nu-command/src/commands/dataframe/series/is_null.rs +++ b/crates/nu-command/src/commands/dataframe/series/is_null.rs @@ -27,8 +27,8 @@ impl WholeStreamCommand for DataFrame { vec![Example { description: "Create mask where values are null", example: r#"let s = ([5 6 0 8] | dataframe to-series); -let res = ($s / $s); -$res | dataframe is-null"#, + let res = ($s / $s); + $res | dataframe is-null"#, result: None, }] } diff --git a/crates/nu-command/src/commands/dataframe/series/mod.rs b/crates/nu-command/src/commands/dataframe/series/mod.rs index 11517d91a0..d6181102ae 100644 --- a/crates/nu-command/src/commands/dataframe/series/mod.rs +++ b/crates/nu-command/src/commands/dataframe/series/mod.rs @@ -12,6 +12,7 @@ pub mod is_null; pub mod is_unique; pub mod n_null; pub mod n_unique; +pub mod not; pub mod rename; pub mod set; pub mod shift; @@ -32,6 +33,7 @@ pub use is_null::DataFrame as DataFrameIsNull; pub use is_unique::DataFrame as DataFrameIsUnique; pub use n_null::DataFrame as DataFrameNNull; pub use n_unique::DataFrame as DataFrameNUnique; +pub use not::DataFrame as DataFrameNot; pub use rename::DataFrame as DataFrameSeriesRename; pub use set::DataFrame as DataFrameSet; pub use shift::DataFrame as DataFrameShift; diff --git a/crates/nu-command/src/commands/dataframe/series/n_null.rs b/crates/nu-command/src/commands/dataframe/series/n_null.rs index 0319f9ffd3..3d3bf66ef7 100644 --- a/crates/nu-command/src/commands/dataframe/series/n_null.rs +++ b/crates/nu-command/src/commands/dataframe/series/n_null.rs @@ -28,7 +28,7 @@ impl WholeStreamCommand for DataFrame { vec![Example { description: "Counts null values", example: r#"let s = ([1 1 0 0 3 3 4] | dataframe to-series); -($s / ss) | dataframe count-null"#, + ($s / ss) | dataframe count-null"#, result: None, }] } diff --git a/crates/nu-command/src/commands/dataframe/series/not.rs b/crates/nu-command/src/commands/dataframe/series/not.rs new file mode 100644 index 0000000000..fe1ef49940 --- /dev/null +++ b/crates/nu-command/src/commands/dataframe/series/not.rs @@ -0,0 +1,55 @@ +use crate::{commands::dataframe::utils::parse_polars_error, prelude::*}; +use nu_engine::WholeStreamCommand; +use nu_errors::ShellError; +use nu_protocol::{dataframe::NuSeries, Signature}; +use polars::prelude::IntoSeries; +use std::ops::Not; + +pub struct DataFrame; + +impl WholeStreamCommand for DataFrame { + fn name(&self) -> &str { + "dataframe not" + } + + fn usage(&self) -> &str { + "[Series] Inverts boolean mask" + } + + fn signature(&self) -> Signature { + Signature::build("dataframe not") + } + + fn run(&self, args: CommandArgs) -> Result { + command(args) + } + + fn examples(&self) -> Vec { + vec![Example { + description: "Inverts boolean mask", + example: "[$true $false $true] | dataframe to-series | dataframe not", + result: None, + }] + } +} + +fn command(mut args: CommandArgs) -> Result { + let tag = args.call_info.name_tag.clone(); + + let series = NuSeries::try_from_stream(&mut args.input, &tag.span)?; + + let bool = series.as_ref().bool().map_err(|e| { + parse_polars_error::<&str>( + &e, + &tag.span, + Some("not only works with series of type bool"), + ) + })?; + + let res = bool.not(); + + Ok(OutputStream::one(NuSeries::series_to_value( + res.into_series(), + tag, + ))) +} diff --git a/crates/nu-command/src/commands/dataframe/series/set.rs b/crates/nu-command/src/commands/dataframe/series/set.rs index f3915299b7..6a21b04409 100644 --- a/crates/nu-command/src/commands/dataframe/series/set.rs +++ b/crates/nu-command/src/commands/dataframe/series/set.rs @@ -34,8 +34,8 @@ impl WholeStreamCommand for DataFrame { vec![Example { description: "Shifts the values by a given period", example: r#"let s = ([1 2 2 3 3] | dataframe to-series | dataframe shift 2); -let mask = ($s | dataframe is-null); -$s | dataframe set 0 --mask $mask"#, + let mask = ($s | dataframe is-null); + $s | dataframe set 0 --mask $mask"#, result: None, }] } diff --git a/crates/nu-command/src/commands/dataframe/sort.rs b/crates/nu-command/src/commands/dataframe/sort.rs index b4f09b2516..dd9b14b649 100644 --- a/crates/nu-command/src/commands/dataframe/sort.rs +++ b/crates/nu-command/src/commands/dataframe/sort.rs @@ -20,12 +20,8 @@ impl WholeStreamCommand for DataFrame { fn signature(&self) -> Signature { Signature::build("dataframe sort") - .optional( - "columns", - SyntaxShape::Table, - "column names to sort dataframe", - ) .switch("reverse", "invert sort", Some('r')) + .rest(SyntaxShape::Any, "column names to sort dataframe") } fn run(&self, args: CommandArgs) -> Result { @@ -36,7 +32,7 @@ impl WholeStreamCommand for DataFrame { vec![ Example { description: "Create new sorted dataframe", - example: "[[a b]; [3 4] [1 2]] | dataframe to-df | dataframe sort [a]", + example: "[[a b]; [3 4] [1 2]] | dataframe to-df | dataframe sort a", result: None, }, Example { @@ -59,24 +55,23 @@ fn command(mut args: CommandArgs) -> Result { match value.value { UntaggedValue::DataFrame(PolarsData::EagerDataFrame(df)) => { - let columns: Option> = args.opt(0)?; + let columns: Vec = args.rest(0)?; - match columns { - Some(columns) => { - let (col_string, col_span) = convert_columns(&columns, &tag)?; + if !columns.is_empty() { + let (col_string, col_span) = convert_columns(&columns, &tag)?; - let res = df - .as_ref() - .sort(&col_string, reverse) - .map_err(|e| parse_polars_error::<&str>(&e, &col_span, None))?; + let res = df + .as_ref() + .sort(&col_string, reverse) + .map_err(|e| parse_polars_error::<&str>(&e, &col_span, None))?; - Ok(OutputStream::one(NuDataFrame::dataframe_to_value(res, tag))) - } - None => Err(ShellError::labeled_error( + Ok(OutputStream::one(NuDataFrame::dataframe_to_value(res, tag))) + } else { + Err(ShellError::labeled_error( "Missing columns", "missing column name to perform sort", &tag.span, - )), + )) } } UntaggedValue::DataFrame(PolarsData::Series(series)) => { diff --git a/crates/nu-command/src/commands/mod.rs b/crates/nu-command/src/commands/mod.rs index 8afaf56cec..9325ae6377 100644 --- a/crates/nu-command/src/commands/mod.rs +++ b/crates/nu-command/src/commands/mod.rs @@ -28,13 +28,13 @@ pub use dataframe::{ DataFrame, DataFrameAggregate, DataFrameAllFalse, DataFrameAllTrue, DataFrameArgMax, DataFrameArgMin, DataFrameArgSort, DataFrameArgTrue, DataFrameArgUnique, DataFrameColumn, DataFrameDTypes, DataFrameDrop, DataFrameDropDuplicates, DataFrameDropNulls, DataFrameDummies, - DataFrameFilter, DataFrameGet, DataFrameGroupBy, DataFrameHead, DataFrameIsDuplicated, + DataFrameFilter, DataFrameFirst, DataFrameGet, DataFrameGroupBy, DataFrameIsDuplicated, DataFrameIsIn, DataFrameIsNotNull, DataFrameIsNull, DataFrameIsUnique, DataFrameJoin, - DataFrameList, DataFrameLoad, DataFrameMelt, DataFrameNNull, DataFrameNUnique, DataFramePivot, - DataFrameSample, DataFrameSelect, DataFrameSeriesRename, DataFrameSet, DataFrameShift, - DataFrameShow, DataFrameSlice, DataFrameSort, DataFrameTail, DataFrameToCsv, DataFrameToDF, - DataFrameToParquet, DataFrameToSeries, DataFrameUnique, DataFrameValueCounts, DataFrameWhere, - DataFrameWithColumn, + DataFrameLast, DataFrameList, DataFrameMelt, DataFrameNNull, DataFrameNUnique, DataFrameNot, + DataFrameOpen, DataFramePivot, DataFrameSample, DataFrameSelect, DataFrameSeriesRename, + DataFrameSet, DataFrameShift, DataFrameShow, DataFrameSlice, DataFrameSort, DataFrameToCsv, + DataFrameToDF, DataFrameToParquet, DataFrameToSeries, DataFrameUnique, DataFrameValueCounts, + DataFrameWhere, DataFrameWithColumn, }; pub use env::*; pub use filesystem::*; diff --git a/crates/nu-command/src/default_context.rs b/crates/nu-command/src/default_context.rs index 441b44f594..b12cf4d8f8 100644 --- a/crates/nu-command/src/default_context.rs +++ b/crates/nu-command/src/default_context.rs @@ -268,7 +268,7 @@ pub fn create_default_context(interactive: bool) -> Result Result Result