diff --git a/crates/nu-command/src/commands.rs b/crates/nu-command/src/commands.rs index 9309df3b7..de52c5df5 100644 --- a/crates/nu-command/src/commands.rs +++ b/crates/nu-command/src/commands.rs @@ -192,11 +192,11 @@ pub(crate) use any::Command as Any; #[cfg(feature = "dataframe")] pub(crate) use dataframe::{ DataFrame, DataFrameAggregate, DataFrameColumn, DataFrameDTypes, DataFrameDrop, - DataFrameDropDuplicates, DataFrameDropNulls, DataFrameDummies, DataFrameGet, DataFrameGroupBy, - DataFrameHead, DataFrameJoin, DataFrameList, DataFrameLoad, DataFrameMelt, DataFramePivot, - DataFrameSample, DataFrameSelect, DataFrameShow, DataFrameSlice, DataFrameSort, DataFrameTail, - DataFrameToCsv, DataFrameToDF, DataFrameToParquet, DataFrameToSeries, DataFrameWhere, - DataFrameWithColumn, + DataFrameDropDuplicates, DataFrameDropNulls, DataFrameDummies, DataFrameFilter, DataFrameGet, + DataFrameGroupBy, DataFrameHead, DataFrameJoin, DataFrameList, DataFrameLoad, DataFrameMelt, + DataFramePivot, DataFrameSample, DataFrameSelect, DataFrameSeriesRename, DataFrameShow, + DataFrameSlice, DataFrameSort, DataFrameTail, DataFrameToCsv, DataFrameToDF, + DataFrameToParquet, DataFrameToSeries, DataFrameWhere, DataFrameWithColumn, }; pub(crate) use enter::Enter; pub(crate) use every::Every; diff --git a/crates/nu-command/src/commands/dataframe/aggregate.rs b/crates/nu-command/src/commands/dataframe/aggregate.rs index 1a0f3a311..4279765f5 100644 --- a/crates/nu-command/src/commands/dataframe/aggregate.rs +++ b/crates/nu-command/src/commands/dataframe/aggregate.rs @@ -77,7 +77,7 @@ pub struct DataFrame; impl WholeStreamCommand for DataFrame { fn name(&self) -> &str { - "pls aggregate" + "dataframe aggregate" } fn usage(&self) -> &str { @@ -85,7 +85,7 @@ impl WholeStreamCommand for DataFrame { } fn signature(&self) -> Signature { - Signature::build("pls aggregate") + Signature::build("dataframe aggregate") .required("operation", SyntaxShape::String, "aggregate operation") .optional( "selection", @@ -109,12 +109,12 @@ 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]] | pls to-df | pls groupby [a] | pls aggregate sum", + "[[a b]; [one 1] [one 2]] | dataframe to-df | dataframe groupby [a] | dataframe aggregate sum", result: None, }, Example { description: "Aggregate sum in dataframe columns", - example: "[[a b]; [4 1] [5 2]] | pls to-df | pls aggregate sum", + example: "[[a b]; [4 1] [5 2]] | dataframe to-df | dataframe aggregate sum", result: None, }, ] diff --git a/crates/nu-command/src/commands/dataframe/column.rs b/crates/nu-command/src/commands/dataframe/column.rs index a17891325..9c7f6d107 100644 --- a/crates/nu-command/src/commands/dataframe/column.rs +++ b/crates/nu-command/src/commands/dataframe/column.rs @@ -13,7 +13,7 @@ pub struct DataFrame; impl WholeStreamCommand for DataFrame { fn name(&self) -> &str { - "pls column" + "dataframe column" } fn usage(&self) -> &str { @@ -21,7 +21,7 @@ impl WholeStreamCommand for DataFrame { } fn signature(&self) -> Signature { - Signature::build("pls column").required("column", SyntaxShape::String, "column name") + Signature::build("dataframe column").required("column", SyntaxShape::String, "column name") } fn run(&self, args: CommandArgs) -> Result { @@ -31,7 +31,7 @@ impl WholeStreamCommand for DataFrame { fn examples(&self) -> Vec { vec![Example { description: "Returns the selected column as series", - example: "[[a b]; [1 2] [3 4]] | pls to-df | pls column a", + example: "[[a b]; [1 2] [3 4]] | dataframe to-df | dataframe column a", result: None, }] } diff --git a/crates/nu-command/src/commands/dataframe/command.rs b/crates/nu-command/src/commands/dataframe/command.rs index 4e3bdb4d0..09ea55aba 100644 --- a/crates/nu-command/src/commands/dataframe/command.rs +++ b/crates/nu-command/src/commands/dataframe/command.rs @@ -7,7 +7,7 @@ pub struct Command; impl WholeStreamCommand for Command { fn name(&self) -> &str { - "pls" + "dataframe" } fn usage(&self) -> &str { @@ -15,7 +15,7 @@ impl WholeStreamCommand for Command { } fn signature(&self) -> Signature { - Signature::build("pls") + Signature::build("dataframe") } fn run(&self, args: CommandArgs) -> Result { diff --git a/crates/nu-command/src/commands/dataframe/drop.rs b/crates/nu-command/src/commands/dataframe/drop.rs index 33557ef7c..9e63b0558 100644 --- a/crates/nu-command/src/commands/dataframe/drop.rs +++ b/crates/nu-command/src/commands/dataframe/drop.rs @@ -9,7 +9,7 @@ pub struct DataFrame; impl WholeStreamCommand for DataFrame { fn name(&self) -> &str { - "pls drop" + "dataframe drop" } fn usage(&self) -> &str { @@ -17,7 +17,7 @@ impl WholeStreamCommand for DataFrame { } fn signature(&self) -> Signature { - Signature::build("pls drop").required( + Signature::build("dataframe drop").required( "columns", SyntaxShape::Table, "column names to be dropped", @@ -31,7 +31,7 @@ impl WholeStreamCommand for DataFrame { fn examples(&self) -> Vec { vec![Example { description: "drop column a", - example: "[[a b]; [1 2] [3 4]] | pls to-df | pls drop [a]", + example: "[[a b]; [1 2] [3 4]] | dataframe to-df | dataframe drop [a]", result: None, }] } diff --git a/crates/nu-command/src/commands/dataframe/drop_duplicates.rs b/crates/nu-command/src/commands/dataframe/drop_duplicates.rs index 635afda92..2c0d7b33c 100644 --- a/crates/nu-command/src/commands/dataframe/drop_duplicates.rs +++ b/crates/nu-command/src/commands/dataframe/drop_duplicates.rs @@ -9,7 +9,7 @@ pub struct DataFrame; impl WholeStreamCommand for DataFrame { fn name(&self) -> &str { - "pls drop-duplicates" + "dataframe drop-duplicates" } fn usage(&self) -> &str { @@ -17,7 +17,7 @@ impl WholeStreamCommand for DataFrame { } fn signature(&self) -> Signature { - Signature::build("pls drop-duplicates") + Signature::build("dataframe drop-duplicates") .optional( "subset", SyntaxShape::Table, @@ -33,7 +33,7 @@ impl WholeStreamCommand for DataFrame { fn examples(&self) -> Vec { vec![Example { description: "drop duplicates", - example: "[[a b]; [1 2] [3 4] [1 2]] | pls to-df | pls drop-duplicates", + example: "[[a b]; [1 2] [3 4] [1 2]] | dataframe to-df | dataframe drop-duplicates", result: None, }] } diff --git a/crates/nu-command/src/commands/dataframe/drop_nulls.rs b/crates/nu-command/src/commands/dataframe/drop_nulls.rs index 24df7b2b2..c081efe96 100644 --- a/crates/nu-command/src/commands/dataframe/drop_nulls.rs +++ b/crates/nu-command/src/commands/dataframe/drop_nulls.rs @@ -9,7 +9,7 @@ pub struct DataFrame; impl WholeStreamCommand for DataFrame { fn name(&self) -> &str { - "pls drop-nulls" + "dataframe drop-nulls" } fn usage(&self) -> &str { @@ -17,7 +17,7 @@ impl WholeStreamCommand for DataFrame { } fn signature(&self) -> Signature { - Signature::build("pls drop-nulls").optional( + Signature::build("dataframe drop-nulls").optional( "subset", SyntaxShape::Table, "subset of columns to drop duplicates", @@ -31,7 +31,7 @@ impl WholeStreamCommand for DataFrame { fn examples(&self) -> Vec { vec![Example { description: "drop null values duplicates", - example: "[[a b]; [1 2] [3 4] [1 2]] | pls to-df | pls drop-nulls", + example: "[[a b]; [1 2] [3 4] [1 2]] | dataframe to-df | dataframe drop-nulls", result: None, }] } diff --git a/crates/nu-command/src/commands/dataframe/dtypes.rs b/crates/nu-command/src/commands/dataframe/dtypes.rs index 7ecaae80b..12d4d0107 100644 --- a/crates/nu-command/src/commands/dataframe/dtypes.rs +++ b/crates/nu-command/src/commands/dataframe/dtypes.rs @@ -7,7 +7,7 @@ pub struct DataFrame; impl WholeStreamCommand for DataFrame { fn name(&self) -> &str { - "pls dtypes" + "dataframe dtypes" } fn usage(&self) -> &str { @@ -15,7 +15,7 @@ impl WholeStreamCommand for DataFrame { } fn signature(&self) -> Signature { - Signature::build("pls dtypes") + Signature::build("dataframe dtypes") } fn run(&self, args: CommandArgs) -> Result { @@ -25,7 +25,7 @@ impl WholeStreamCommand for DataFrame { fn examples(&self) -> Vec { vec![Example { description: "drop column a", - example: "[[a b]; [1 2] [3 4]] | pls to-df | pls dtypes", + example: "[[a b]; [1 2] [3 4]] | dataframe to-df | dataframe dtypes", result: None, }] } diff --git a/crates/nu-command/src/commands/dataframe/dummies.rs b/crates/nu-command/src/commands/dataframe/dummies.rs index ed5d49239..1542e11bf 100644 --- a/crates/nu-command/src/commands/dataframe/dummies.rs +++ b/crates/nu-command/src/commands/dataframe/dummies.rs @@ -9,7 +9,7 @@ pub struct DataFrame; impl WholeStreamCommand for DataFrame { fn name(&self) -> &str { - "pls to-dummies" + "dataframe to-dummies" } fn usage(&self) -> &str { @@ -17,7 +17,7 @@ impl WholeStreamCommand for DataFrame { } fn signature(&self) -> Signature { - Signature::build("pls to-dummies") + Signature::build("dataframe to-dummies") } fn run(&self, args: CommandArgs) -> Result { @@ -27,7 +27,7 @@ impl WholeStreamCommand for DataFrame { fn examples(&self) -> Vec { vec![Example { description: "Create new dataframe with dummy variables", - example: "[[a b]; [1 2] [3 4]] | pls to-df | pls to-dummies", + example: "[[a b]; [1 2] [3 4]] | dataframe to-df | dataframe to-dummies", result: None, }] } diff --git a/crates/nu-command/src/commands/dataframe/filter.rs b/crates/nu-command/src/commands/dataframe/filter.rs new file mode 100644 index 000000000..e63835c77 --- /dev/null +++ b/crates/nu-command/src/commands/dataframe/filter.rs @@ -0,0 +1,79 @@ +use crate::prelude::*; +use nu_engine::WholeStreamCommand; +use nu_errors::ShellError; +use nu_protocol::{ + dataframe::{NuDataFrame, PolarsData}, + Signature, SyntaxShape, UntaggedValue, Value, +}; + +use super::utils::parse_polars_error; +pub struct DataFrame; + +impl WholeStreamCommand for DataFrame { + fn name(&self) -> &str { + "dataframe filter" + } + + fn usage(&self) -> &str { + "Filters dataframe using a mask as reference" + } + + fn signature(&self) -> Signature { + Signature::build("dataframe filter") + .required("with", SyntaxShape::String, "the word 'with'") + .required("mask", SyntaxShape::Any, "boolean mask used to filter data") + } + + fn run(&self, args: CommandArgs) -> Result { + command(args) + } + + fn examples(&self) -> Vec { + vec![ + 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"#, + 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"#, + result: None, + }, + ] + } +} + +fn command(mut args: CommandArgs) -> Result { + let tag = args.call_info.name_tag.clone(); + let value: Value = args.req(1)?; + + let series_span = value.tag.span.clone(); + let series = match value.value { + UntaggedValue::DataFrame(PolarsData::Series(series)) => Ok(series), + _ => Err(ShellError::labeled_error( + "Incorrect type", + "can only add a series to a dataframe", + value.tag.span, + )), + }?; + + let casted = series.as_ref().bool().map_err(|e| { + parse_polars_error( + &e, + &&series_span, + Some("Perhaps you want to use a series with booleans as mask"), + ) + })?; + + let df = NuDataFrame::try_from_stream(&mut args.input, &tag.span)?; + + let res = df + .as_ref() + .filter(&casted) + .map_err(|e| parse_polars_error::<&str>(&e, &tag.span, None))?; + + Ok(OutputStream::one(NuDataFrame::dataframe_to_value(res, tag))) +} diff --git a/crates/nu-command/src/commands/dataframe/get.rs b/crates/nu-command/src/commands/dataframe/get.rs index 695740bdf..0f5be9956 100644 --- a/crates/nu-command/src/commands/dataframe/get.rs +++ b/crates/nu-command/src/commands/dataframe/get.rs @@ -8,7 +8,7 @@ pub struct DataFrame; impl WholeStreamCommand for DataFrame { fn name(&self) -> &str { - "pls get" + "dataframe get" } fn usage(&self) -> &str { @@ -16,7 +16,7 @@ impl WholeStreamCommand for DataFrame { } fn signature(&self) -> Signature { - Signature::build("pls get").required( + Signature::build("dataframe get").required( "columns", SyntaxShape::Table, "column names to sort dataframe", @@ -30,7 +30,7 @@ impl WholeStreamCommand for DataFrame { fn examples(&self) -> Vec { vec![Example { description: "Creates dataframe with selected columns", - example: "[[a b]; [1 2] [3 4]] | pls to-df | pls get [a]", + example: "[[a b]; [1 2] [3 4]] | dataframe to-df | dataframe get [a]", result: None, }] } diff --git a/crates/nu-command/src/commands/dataframe/groupby.rs b/crates/nu-command/src/commands/dataframe/groupby.rs index 8af40e392..e0081898e 100644 --- a/crates/nu-command/src/commands/dataframe/groupby.rs +++ b/crates/nu-command/src/commands/dataframe/groupby.rs @@ -12,7 +12,7 @@ pub struct DataFrame; impl WholeStreamCommand for DataFrame { fn name(&self) -> &str { - "pls group-by" + "dataframe group-by" } fn usage(&self) -> &str { @@ -20,7 +20,7 @@ impl WholeStreamCommand for DataFrame { } fn signature(&self) -> Signature { - Signature::build("pls group-by").required( + Signature::build("dataframe group-by").required( "by columns", SyntaxShape::Table, "groupby columns", @@ -34,7 +34,7 @@ impl WholeStreamCommand for DataFrame { fn examples(&self) -> Vec { vec![Example { description: "Grouping by column a", - example: "[[a b]; [one 1] [one 2]] | pls to-df | pls group-by [a]", + example: "[[a b]; [one 1] [one 2]] | dataframe to-df | dataframe group-by [a]", result: None, }] } diff --git a/crates/nu-command/src/commands/dataframe/head.rs b/crates/nu-command/src/commands/dataframe/head.rs index e9e4317ea..1de35a775 100644 --- a/crates/nu-command/src/commands/dataframe/head.rs +++ b/crates/nu-command/src/commands/dataframe/head.rs @@ -9,7 +9,7 @@ pub struct DataFrame; impl WholeStreamCommand for DataFrame { fn name(&self) -> &str { - "pls head" + "dataframe head" } fn usage(&self) -> &str { @@ -17,7 +17,7 @@ impl WholeStreamCommand for DataFrame { } fn signature(&self) -> Signature { - Signature::build("pls select").optional( + Signature::build("dataframe select").optional( "rows", SyntaxShape::Number, "Number of rows for head", @@ -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]] | pls to-df | pls head", + example: "[[a b]; [1 2] [3 4]] | dataframe to-df | dataframe head", result: None, }] } diff --git a/crates/nu-command/src/commands/dataframe/join.rs b/crates/nu-command/src/commands/dataframe/join.rs index f7206cd36..326397273 100644 --- a/crates/nu-command/src/commands/dataframe/join.rs +++ b/crates/nu-command/src/commands/dataframe/join.rs @@ -16,7 +16,7 @@ pub struct DataFrame; impl WholeStreamCommand for DataFrame { fn name(&self) -> &str { - "pls join" + "dataframe join" } fn usage(&self) -> &str { @@ -24,7 +24,7 @@ impl WholeStreamCommand for DataFrame { } fn signature(&self) -> Signature { - Signature::build("pls join") + Signature::build("dataframe join") .required("dataframe", SyntaxShape::Any, "right dataframe to join") .required( "l_columns", @@ -52,13 +52,13 @@ impl WholeStreamCommand for DataFrame { vec![ Example { description: "inner join dataframe", - example: "echo [[a b]; [1 2] [3 4]] | pls to-df | pls join $right [a] [a]", + example: "echo [[a b]; [1 2] [3 4]] | dataframe to-df | dataframe join $right [a] [a]", result: None, }, Example { description: "right join dataframe", example: - "[[a b]; [1 2] [3 4] [5 6]] | pls to-df | pls join $right [b] [b] -t right", + "[[a b]; [1 2] [3 4] [5 6]] | dataframe to-df | dataframe join $right [b] [b] -t right", result: None, }, ] diff --git a/crates/nu-command/src/commands/dataframe/list.rs b/crates/nu-command/src/commands/dataframe/list.rs index f059f349a..33c9ecd6e 100644 --- a/crates/nu-command/src/commands/dataframe/list.rs +++ b/crates/nu-command/src/commands/dataframe/list.rs @@ -7,7 +7,7 @@ pub struct DataFrame; impl WholeStreamCommand for DataFrame { fn name(&self) -> &str { - "pls list" + "dataframe list" } fn usage(&self) -> &str { @@ -15,7 +15,7 @@ impl WholeStreamCommand for DataFrame { } fn signature(&self) -> Signature { - Signature::build("pls list") + Signature::build("dataframe list") } fn run(&self, args: CommandArgs) -> Result { @@ -57,7 +57,7 @@ impl WholeStreamCommand for DataFrame { fn examples(&self) -> Vec { vec![Example { description: "Lists loaded dataframes in current scope", - example: "pls list", + example: "dataframe list", result: None, }] } diff --git a/crates/nu-command/src/commands/dataframe/load.rs b/crates/nu-command/src/commands/dataframe/load.rs index 19316ecf5..214a1dacb 100644 --- a/crates/nu-command/src/commands/dataframe/load.rs +++ b/crates/nu-command/src/commands/dataframe/load.rs @@ -15,7 +15,7 @@ pub struct DataFrame; impl WholeStreamCommand for DataFrame { fn name(&self) -> &str { - "pls load" + "dataframe load" } fn usage(&self) -> &str { @@ -23,7 +23,7 @@ impl WholeStreamCommand for DataFrame { } fn signature(&self) -> Signature { - Signature::build("pls load") + Signature::build("dataframe load") .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: "pls load test.csv", + example: "dataframe load test.csv", result: None, }] } diff --git a/crates/nu-command/src/commands/dataframe/melt.rs b/crates/nu-command/src/commands/dataframe/melt.rs index 7156b2ff0..7f5e0ea0e 100644 --- a/crates/nu-command/src/commands/dataframe/melt.rs +++ b/crates/nu-command/src/commands/dataframe/melt.rs @@ -9,7 +9,7 @@ pub struct DataFrame; impl WholeStreamCommand for DataFrame { fn name(&self) -> &str { - "pls melt" + "dataframe melt" } fn usage(&self) -> &str { @@ -17,7 +17,7 @@ impl WholeStreamCommand for DataFrame { } fn signature(&self) -> Signature { - Signature::build("pls melt") + Signature::build("dataframe melt") .required("id_columns", SyntaxShape::Table, "Id columns for melting") .required( "value_columns", @@ -33,7 +33,7 @@ impl WholeStreamCommand for DataFrame { fn examples(&self) -> Vec { vec![Example { description: "melt dataframe", - example: "[[a b]; [a 2] [b 4] [a 6]] | pls to-df | pls melt [a] [b]", + example: "[[a b]; [a 2] [b 4] [a 6]] | dataframe to-df | dataframe melt [a] [b]", result: None, }] } diff --git a/crates/nu-command/src/commands/dataframe/mod.rs b/crates/nu-command/src/commands/dataframe/mod.rs index 6f113bbc4..487758c8d 100644 --- a/crates/nu-command/src/commands/dataframe/mod.rs +++ b/crates/nu-command/src/commands/dataframe/mod.rs @@ -6,6 +6,7 @@ pub mod drop_duplicates; pub mod drop_nulls; pub mod dtypes; pub mod dummies; +pub mod filter; pub mod get; pub mod groupby; pub mod head; @@ -16,6 +17,7 @@ pub mod melt; pub mod pivot; pub mod sample; pub mod select; +pub mod series_rename; pub mod show; pub mod slice; pub mod sort; @@ -36,6 +38,7 @@ pub use drop_duplicates::DataFrame as DataFrameDropDuplicates; 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 get::DataFrame as DataFrameGet; pub use groupby::DataFrame as DataFrameGroupBy; pub use head::DataFrame as DataFrameHead; @@ -46,6 +49,7 @@ pub use melt::DataFrame as DataFrameMelt; pub use pivot::DataFrame as DataFramePivot; pub use sample::DataFrame as DataFrameSample; pub use select::DataFrame as DataFrameSelect; +pub use series_rename::DataFrame as DataFrameSeriesRename; pub use show::DataFrame as DataFrameShow; pub use slice::DataFrame as DataFrameSlice; pub use sort::DataFrame as DataFrameSort; diff --git a/crates/nu-command/src/commands/dataframe/pivot.rs b/crates/nu-command/src/commands/dataframe/pivot.rs index 94e247c10..97c8eb266 100644 --- a/crates/nu-command/src/commands/dataframe/pivot.rs +++ b/crates/nu-command/src/commands/dataframe/pivot.rs @@ -42,7 +42,7 @@ pub struct DataFrame; impl WholeStreamCommand for DataFrame { fn name(&self) -> &str { - "pls pivot" + "dataframe pivot" } fn usage(&self) -> &str { @@ -50,7 +50,7 @@ impl WholeStreamCommand for DataFrame { } fn signature(&self) -> Signature { - Signature::build("pls pivot") + Signature::build("dataframe pivot") .required( "pivot column", SyntaxShape::String, @@ -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]] | pls to-df | pls group-by [a] | pls 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 11c42810e..aadc936f9 100644 --- a/crates/nu-command/src/commands/dataframe/sample.rs +++ b/crates/nu-command/src/commands/dataframe/sample.rs @@ -9,7 +9,7 @@ pub struct DataFrame; impl WholeStreamCommand for DataFrame { fn name(&self) -> &str { - "pls sample" + "dataframe sample" } fn usage(&self) -> &str { @@ -17,7 +17,7 @@ impl WholeStreamCommand for DataFrame { } fn signature(&self) -> Signature { - Signature::build("pls load") + Signature::build("dataframe load") .named( "n_rows", SyntaxShape::Number, @@ -41,12 +41,13 @@ impl WholeStreamCommand for DataFrame { vec![ Example { description: "Sample rows from dataframe", - example: "[[a b]; [1 2] [3 4]] | pls to-df | pls sample -r 1", + example: "[[a b]; [1 2] [3 4]] | dataframe to-df | dataframe sample -r 1", result: None, }, Example { description: "Shows sample row using fraction and replace", - example: "[[a b]; [1 2] [3 4] [5 6]] | pls to-df | pls sample -f 0.5 -e", + example: + "[[a b]; [1 2] [3 4] [5 6]] | dataframe to-df | dataframe sample -f 0.5 -e", result: None, }, ] diff --git a/crates/nu-command/src/commands/dataframe/select.rs b/crates/nu-command/src/commands/dataframe/select.rs index 48b008632..6fe9fa9b1 100644 --- a/crates/nu-command/src/commands/dataframe/select.rs +++ b/crates/nu-command/src/commands/dataframe/select.rs @@ -9,7 +9,7 @@ pub struct DataFrame; impl WholeStreamCommand for DataFrame { fn name(&self) -> &str { - "pls select" + "dataframe select" } fn usage(&self) -> &str { @@ -17,7 +17,7 @@ impl WholeStreamCommand for DataFrame { } fn signature(&self) -> Signature { - Signature::build("pls select").required( + Signature::build("dataframe select").required( "columns", SyntaxShape::Table, "selected column names", @@ -31,7 +31,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]] | pls to-df | pls select [a]", + example: "[[a b]; [1 2] [3 4]] | dataframe to-df | dataframe select [a]", result: None, }] } diff --git a/crates/nu-command/src/commands/dataframe/series_rename.rs b/crates/nu-command/src/commands/dataframe/series_rename.rs new file mode 100644 index 000000000..1a39fa49d --- /dev/null +++ b/crates/nu-command/src/commands/dataframe/series_rename.rs @@ -0,0 +1,48 @@ +use crate::prelude::*; +use nu_engine::WholeStreamCommand; +use nu_errors::ShellError; +use nu_protocol::{dataframe::NuSeries, Signature, SyntaxShape}; +use nu_source::Tagged; + +pub struct DataFrame; + +impl WholeStreamCommand for DataFrame { + fn name(&self) -> &str { + "dataframe rename-series" + } + + fn usage(&self) -> &str { + "Renames a series" + } + + fn signature(&self) -> Signature { + Signature::build("dataframe rename-series").required( + "name", + SyntaxShape::String, + "new series name", + ) + } + + fn run(&self, args: CommandArgs) -> Result { + command(args) + } + + fn examples(&self) -> Vec { + vec![Example { + description: "Renames a series", + example: "[5 6 7 8] | dataframe to-series | dataframe rename-series new_name", + result: None, + }] + } +} + +fn command(mut args: CommandArgs) -> Result { + let tag = args.call_info.name_tag.clone(); + let name: Tagged = args.req(0)?; + + let mut series = NuSeries::try_from_stream(&mut args.input, &tag.span)?; + + series.as_mut().rename(name.item.as_ref()); + + Ok(OutputStream::one(series.to_value(tag))) +} diff --git a/crates/nu-command/src/commands/dataframe/show.rs b/crates/nu-command/src/commands/dataframe/show.rs index 95ffcf2dc..0e56b0630 100644 --- a/crates/nu-command/src/commands/dataframe/show.rs +++ b/crates/nu-command/src/commands/dataframe/show.rs @@ -9,7 +9,7 @@ pub struct DataFrame; impl WholeStreamCommand for DataFrame { fn name(&self) -> &str { - "pls show" + "dataframe show" } fn usage(&self) -> &str { @@ -17,7 +17,7 @@ impl WholeStreamCommand for DataFrame { } fn signature(&self) -> Signature { - Signature::build("pls show") + Signature::build("dataframe show") .named( "n_rows", SyntaxShape::Number, @@ -35,12 +35,12 @@ impl WholeStreamCommand for DataFrame { vec![ Example { description: "Shows head rows from dataframe", - example: "[[a b]; [1 2] [3 4]] | pls to-df | pls show", + example: "[[a b]; [1 2] [3 4]] | dataframe to-df | dataframe show", result: None, }, Example { description: "Shows tail rows from dataframe", - example: "[[a b]; [1 2] [3 4] [5 6]] | pls to-df | pls show -t -n 1", + example: "[[a b]; [1 2] [3 4] [5 6]] | dataframe to-df | dataframe show -t -n 1", result: None, }, ] diff --git a/crates/nu-command/src/commands/dataframe/slice.rs b/crates/nu-command/src/commands/dataframe/slice.rs index 3dc7f69de..a0d861fc2 100644 --- a/crates/nu-command/src/commands/dataframe/slice.rs +++ b/crates/nu-command/src/commands/dataframe/slice.rs @@ -8,7 +8,7 @@ pub struct DataFrame; impl WholeStreamCommand for DataFrame { fn name(&self) -> &str { - "pls slice" + "dataframe slice" } fn usage(&self) -> &str { @@ -16,7 +16,7 @@ impl WholeStreamCommand for DataFrame { } fn signature(&self) -> Signature { - Signature::build("pls slice") + Signature::build("dataframe slice") .required("offset", SyntaxShape::Number, "start of slice") .required("size", SyntaxShape::Number, "size of slice") } @@ -28,7 +28,7 @@ impl WholeStreamCommand for DataFrame { fn examples(&self) -> Vec { vec![Example { description: "Create new dataframe from a slice of the rows", - example: "[[a b]; [1 2] [3 4]] | pls to-df | pls slice 0 1", + example: "[[a b]; [1 2] [3 4]] | dataframe to-df | dataframe slice 0 1", result: None, }] } diff --git a/crates/nu-command/src/commands/dataframe/sort.rs b/crates/nu-command/src/commands/dataframe/sort.rs index 36f4dcaa5..e1382d8b1 100644 --- a/crates/nu-command/src/commands/dataframe/sort.rs +++ b/crates/nu-command/src/commands/dataframe/sort.rs @@ -8,7 +8,7 @@ pub struct DataFrame; impl WholeStreamCommand for DataFrame { fn name(&self) -> &str { - "pls sort" + "dataframe sort" } fn usage(&self) -> &str { @@ -16,7 +16,7 @@ impl WholeStreamCommand for DataFrame { } fn signature(&self) -> Signature { - Signature::build("pls sort") + Signature::build("dataframe sort") .required( "columns", SyntaxShape::Table, @@ -32,7 +32,7 @@ impl WholeStreamCommand for DataFrame { fn examples(&self) -> Vec { vec![Example { description: "Create new sorted dataframe", - example: "[[a b]; [3 4] [1 2]] | pls to-df | pls sort [a]", + example: "[[a b]; [3 4] [1 2]] | dataframe to-df | dataframe sort [a]", result: None, }] } diff --git a/crates/nu-command/src/commands/dataframe/tail.rs b/crates/nu-command/src/commands/dataframe/tail.rs index 010613e15..e4311bbf3 100644 --- a/crates/nu-command/src/commands/dataframe/tail.rs +++ b/crates/nu-command/src/commands/dataframe/tail.rs @@ -8,7 +8,7 @@ pub struct DataFrame; impl WholeStreamCommand for DataFrame { fn name(&self) -> &str { - "pls tail" + "dataframe tail" } fn usage(&self) -> &str { @@ -16,7 +16,7 @@ impl WholeStreamCommand for DataFrame { } fn signature(&self) -> Signature { - Signature::build("pls tail").optional( + Signature::build("dataframe tail").optional( "n_rows", SyntaxShape::Number, "Number of rows for tail", @@ -30,7 +30,7 @@ impl WholeStreamCommand for DataFrame { fn examples(&self) -> Vec { vec![Example { description: "Create new dataframe with tail rows", - example: "[[a b]; [1 2] [3 4]] | pls to-df | pls tail", + example: "[[a b]; [1 2] [3 4]] | dataframe to-df | dataframe tail", result: None, }] } diff --git a/crates/nu-command/src/commands/dataframe/to_csv.rs b/crates/nu-command/src/commands/dataframe/to_csv.rs index c554ebdf6..6facb57e2 100644 --- a/crates/nu-command/src/commands/dataframe/to_csv.rs +++ b/crates/nu-command/src/commands/dataframe/to_csv.rs @@ -18,7 +18,7 @@ pub struct DataFrame; impl WholeStreamCommand for DataFrame { fn name(&self) -> &str { - "pls to-csv" + "dataframe to-csv" } fn usage(&self) -> &str { @@ -26,7 +26,7 @@ impl WholeStreamCommand for DataFrame { } fn signature(&self) -> Signature { - Signature::build("pls to-csv") + Signature::build("dataframe to-csv") .required("file", SyntaxShape::FilePath, "file path to save dataframe") .named( "delimiter", @@ -45,12 +45,13 @@ impl WholeStreamCommand for DataFrame { vec![ Example { description: "Saves dataframe to csv file", - example: "[[a b]; [1 2] [3 4]] | pls to-df | pls to_csv test.csv", + example: "[[a b]; [1 2] [3 4]] | dataframe to-df | dataframe to_csv test.csv", result: None, }, Example { description: "Saves dataframe to csv file using other delimiter", - example: "[[a b]; [1 2] [3 4]] | pls to-df | pls to-csv test.csv -d '|'", + example: + "[[a b]; [1 2] [3 4]] | dataframe to-df | dataframe to-csv test.csv -d '|'", result: None, }, ] diff --git a/crates/nu-command/src/commands/dataframe/to_df.rs b/crates/nu-command/src/commands/dataframe/to_df.rs index 26ad3ee94..562dd2fa7 100644 --- a/crates/nu-command/src/commands/dataframe/to_df.rs +++ b/crates/nu-command/src/commands/dataframe/to_df.rs @@ -7,7 +7,7 @@ pub struct DataFrame; impl WholeStreamCommand for DataFrame { fn name(&self) -> &str { - "pls to-df" + "dataframe to-df" } fn usage(&self) -> &str { @@ -15,7 +15,7 @@ impl WholeStreamCommand for DataFrame { } fn signature(&self) -> Signature { - Signature::build("pls to-df") + Signature::build("dataframe to-df") } fn run(&self, args: CommandArgs) -> Result { @@ -29,7 +29,7 @@ impl WholeStreamCommand for DataFrame { fn examples(&self) -> Vec { vec![Example { description: "Takes an input stream and converts it to a polars dataframe", - example: "[[a b];[1 2] [3 4]] | pls to-df", + example: "[[a b];[1 2] [3 4]] | dataframe to-df", result: None, }] } diff --git a/crates/nu-command/src/commands/dataframe/to_parquet.rs b/crates/nu-command/src/commands/dataframe/to_parquet.rs index f5ed1ab6a..1dd96e57e 100644 --- a/crates/nu-command/src/commands/dataframe/to_parquet.rs +++ b/crates/nu-command/src/commands/dataframe/to_parquet.rs @@ -16,7 +16,7 @@ pub struct DataFrame; impl WholeStreamCommand for DataFrame { fn name(&self) -> &str { - "pls to-parquet" + "dataframe to-parquet" } fn usage(&self) -> &str { @@ -24,7 +24,7 @@ impl WholeStreamCommand for DataFrame { } fn signature(&self) -> Signature { - Signature::build("pls to-parquet").required( + Signature::build("dataframe to-parquet").required( "file", SyntaxShape::FilePath, "file path to save dataframe", @@ -38,7 +38,7 @@ impl WholeStreamCommand for DataFrame { fn examples(&self) -> Vec { vec![Example { description: "Saves dataframe to parquet file", - example: "[[a b]; [1 2] [3 4]] | pls to-df | pls to-parquet test.parquet", + example: "[[a b]; [1 2] [3 4]] | dataframe to-df | dataframe to-parquet test.parquet", result: None, }] } diff --git a/crates/nu-command/src/commands/dataframe/to_series.rs b/crates/nu-command/src/commands/dataframe/to_series.rs index 627a87474..bbd561ac3 100644 --- a/crates/nu-command/src/commands/dataframe/to_series.rs +++ b/crates/nu-command/src/commands/dataframe/to_series.rs @@ -8,7 +8,7 @@ pub struct DataFrame; impl WholeStreamCommand for DataFrame { fn name(&self) -> &str { - "pls to-series" + "dataframe to-series" } fn usage(&self) -> &str { @@ -16,7 +16,7 @@ impl WholeStreamCommand for DataFrame { } fn signature(&self) -> Signature { - Signature::build("pls to-series").optional( + Signature::build("dataframe to-series").optional( "name", SyntaxShape::String, "Optional series name", @@ -37,7 +37,7 @@ impl WholeStreamCommand for DataFrame { fn examples(&self) -> Vec { vec![Example { description: "Takes an input stream and converts it to a polars series", - example: "[1 2 3 4] | pls to-series my-col", + example: "[1 2 3 4] | dataframe to-series my-col", result: None, }] } diff --git a/crates/nu-command/src/commands/dataframe/where_.rs b/crates/nu-command/src/commands/dataframe/where_.rs index 4c8751dc9..eacd53e54 100644 --- a/crates/nu-command/src/commands/dataframe/where_.rs +++ b/crates/nu-command/src/commands/dataframe/where_.rs @@ -14,11 +14,11 @@ pub struct DataFrame; impl WholeStreamCommand for DataFrame { fn name(&self) -> &str { - "pls where" + "dataframe where" } fn signature(&self) -> Signature { - Signature::build("pls where").required( + Signature::build("dataframe where").required( "condition", SyntaxShape::RowCondition, "the condition that must match", @@ -36,7 +36,7 @@ impl WholeStreamCommand for DataFrame { fn examples(&self) -> Vec { vec![Example { description: "Filter dataframe based on column a", - example: "[[a b]; [1 2] [3 4]] | pls to-df | pls where a == 1", + example: "[[a b]; [1 2] [3 4]] | dataframe to-df | dataframe where a == 1", result: None, }] } diff --git a/crates/nu-command/src/commands/dataframe/with_column.rs b/crates/nu-command/src/commands/dataframe/with_column.rs index 44911509c..2bbd4ae22 100644 --- a/crates/nu-command/src/commands/dataframe/with_column.rs +++ b/crates/nu-command/src/commands/dataframe/with_column.rs @@ -12,7 +12,7 @@ pub struct DataFrame; impl WholeStreamCommand for DataFrame { fn name(&self) -> &str { - "pls with-column" + "dataframe with-column" } fn usage(&self) -> &str { @@ -20,7 +20,7 @@ impl WholeStreamCommand for DataFrame { } fn signature(&self) -> Signature { - Signature::build("pls with-column") + Signature::build("dataframe with-column") .required("series", SyntaxShape::Any, "series to be added") .required("as", SyntaxShape::String, "the word 'as'") .required("name", SyntaxShape::String, "column name") @@ -34,7 +34,7 @@ impl WholeStreamCommand for DataFrame { vec![Example { description: "Adds a series to the dataframe", example: - "[[a b]; [1 2] [3 4]] | pls to-df | pls with-column ([5 6] | pls to-series) as c", + "[[a b]; [1 2] [3 4]] | dataframe to-df | dataframe with-column ([5 6] | dataframe to-series) as c", result: None, }] } @@ -58,13 +58,9 @@ fn command(mut args: CommandArgs) -> Result { let mut df = NuDataFrame::try_from_stream(&mut args.input, &tag.span)?; - let res = df - .as_mut() + df.as_mut() .with_column(series) .map_err(|e| parse_polars_error::<&str>(&e, &tag.span, None))?; - Ok(OutputStream::one(NuDataFrame::dataframe_to_value( - res.clone(), - tag, - ))) + Ok(OutputStream::one(df.to_value(tag))) } diff --git a/crates/nu-command/src/commands/default_context.rs b/crates/nu-command/src/commands/default_context.rs index 4bf7489ee..791a0313e 100644 --- a/crates/nu-command/src/commands/default_context.rs +++ b/crates/nu-command/src/commands/default_context.rs @@ -288,6 +288,8 @@ pub fn create_default_context(interactive: bool) -> Result { + | UntaggedValue::Primitive(Primitive::String(_)) + | UntaggedValue::Primitive(Primitive::Boolean(_)) => { insert_value(value, &mut vec_values)? } _ => { @@ -87,7 +88,7 @@ impl NuSeries { "Format not supported", "Value not supported for conversion", &value.tag.span, - "Perhaps you want to use a list of primitive values (int, decimal, string)", + "Perhaps you want to use a list of primitive values (int, decimal, string, or bool)", &value.tag.span, )); } @@ -153,31 +154,27 @@ macro_rules! series_to_chunked { (size, 0, 0) }; - let head = chunked_array - .into_iter() - .take(head_size) - .map(|value| match value { - Some(v) => { - let mut dictionary_row = Dictionary::default(); - - let value = Value { - value: UntaggedValue::Primitive(v.into()), - tag: Tag::unknown(), - }; - - let header = format!("{} ({})", $self.as_ref().name(), $self.as_ref().dtype()); - dictionary_row.insert(header, value); - - Value { - value: UntaggedValue::Row(dictionary_row), - tag: Tag::unknown(), - } - } + let head = chunked_array.into_iter().take(head_size).map(|value| { + let value = match value { + Some(v) => Value { + value: UntaggedValue::Primitive(v.into()), + tag: Tag::unknown(), + }, None => Value { value: UntaggedValue::Primitive(Primitive::Nothing), tag: Tag::unknown(), }, - }); + }; + + let mut dictionary_row = Dictionary::default(); + let header = format!("{} ({})", $self.as_ref().name(), $self.as_ref().dtype()); + dictionary_row.insert(header, value); + + Value { + value: UntaggedValue::Row(dictionary_row), + tag: Tag::unknown(), + } + }); let res = if $self.as_ref().len() < size { head.collect::>() @@ -285,6 +282,10 @@ fn insert_value(value: Value, vec_values: &mut Vec) -> Result<(), ShellEr | ( UntaggedValue::Primitive(Primitive::String(_)), UntaggedValue::Primitive(Primitive::String(_)), + ) + | ( + UntaggedValue::Primitive(Primitive::Boolean(_)), + UntaggedValue::Primitive(Primitive::Boolean(_)), ) => { vec_values.push(value); Ok(()) @@ -330,6 +331,14 @@ fn from_parsed_vector( }; Series::new(series_name, series_values?) } + UntaggedValue::Primitive(Primitive::Boolean(_)) => { + let series_values: Result, _> = vec_values.iter().map(|v| v.as_bool()).collect(); + let series_name = match &name { + Some(n) => n.as_ref(), + None => "string", + }; + Series::new(series_name, series_values?) + } _ => unreachable!("The untagged type is checked while creating vec_values"), };