From 865906e4508b1b82c1bbc176c2358b4f6358f454 Mon Sep 17 00:00:00 2001 From: Fernando Herrera Date: Fri, 10 Dec 2021 00:17:11 +0000 Subject: [PATCH] Dataframe commands name (#457) * corrected missing shellerror type * batch dataframe commands * removed option to find declaration with input * ordered dataframe folders * dataframe command name --- crates/nu-command/src/dataframe/append.rs | 10 ++--- crates/nu-command/src/dataframe/column.rs | 4 +- crates/nu-command/src/dataframe/command.rs | 37 +++++++++++++++++++ crates/nu-command/src/dataframe/describe.rs | 4 +- crates/nu-command/src/dataframe/drop.rs | 4 +- crates/nu-command/src/dataframe/dtypes.rs | 4 +- crates/nu-command/src/dataframe/mod.rs | 4 ++ crates/nu-command/src/dataframe/open.rs | 4 +- crates/nu-command/src/dataframe/series/mod.rs | 1 + crates/nu-command/src/dataframe/to_df.rs | 10 ++--- crates/nu-engine/src/documentation.rs | 4 +- 11 files changed, 64 insertions(+), 22 deletions(-) create mode 100644 crates/nu-command/src/dataframe/command.rs create mode 100644 crates/nu-command/src/dataframe/series/mod.rs diff --git a/crates/nu-command/src/dataframe/append.rs b/crates/nu-command/src/dataframe/append.rs index 52ad5f3b9..914ff7189 100644 --- a/crates/nu-command/src/dataframe/append.rs +++ b/crates/nu-command/src/dataframe/append.rs @@ -12,7 +12,7 @@ pub struct AppendDF; impl Command for AppendDF { fn name(&self) -> &str { - "append-df" + "dataframe append" } fn usage(&self) -> &str { @@ -30,8 +30,8 @@ impl Command for AppendDF { vec![ Example { description: "Appends a dataframe as new columns", - example: r#"let a = ([[a b]; [1 2] [3 4]] | to df); -$a | append-df $a"#, + example: r#"let a = ([[a b]; [1 2] [3 4]] | dataframe to-df); +$a | dataframe append $a"#, result: Some( NuDataFrame::try_from_columns(vec![ Column::new("a".to_string(), vec![1.into(), 3.into()]), @@ -46,8 +46,8 @@ $a | append-df $a"#, Example { description: "Appends a dataframe merging at the end of columns", //example: r#"let a = ([[a b]; [1 2] [3 4]] | to df); $a | append-df $a -col"#, - example: r#"let a = ([[a b]; [1 2] [3 4]] | to df); -$a | append-df $a --col"#, + example: r#"let a = ([[a b]; [1 2] [3 4]] | dataframe to-df); +$a | dataframe append $a --col"#, result: Some( NuDataFrame::try_from_columns(vec![ Column::new( diff --git a/crates/nu-command/src/dataframe/column.rs b/crates/nu-command/src/dataframe/column.rs index 422dd710c..d0d84c118 100644 --- a/crates/nu-command/src/dataframe/column.rs +++ b/crates/nu-command/src/dataframe/column.rs @@ -12,7 +12,7 @@ pub struct ColumnDF; impl Command for ColumnDF { fn name(&self) -> &str { - "column" + "dataframe column" } fn usage(&self) -> &str { @@ -28,7 +28,7 @@ impl Command for ColumnDF { fn examples(&self) -> Vec { vec![Example { description: "Returns the selected column as series", - example: "[[a b]; [1 2] [3 4]] | to df | column a", + example: "[[a b]; [1 2] [3 4]] | dataframe to-df | dataframe column a", result: Some( NuDataFrame::try_from_columns(vec![Column::new( "a".to_string(), diff --git a/crates/nu-command/src/dataframe/command.rs b/crates/nu-command/src/dataframe/command.rs new file mode 100644 index 000000000..5ea43a2ed --- /dev/null +++ b/crates/nu-command/src/dataframe/command.rs @@ -0,0 +1,37 @@ +use nu_engine::get_full_help; +use nu_protocol::{ + ast::Call, + engine::{Command, EngineState, Stack}, + Category, IntoPipelineData, PipelineData, ShellError, Signature, Value, +}; + +#[derive(Clone)] +pub struct Dataframe; + +impl Command for Dataframe { + fn name(&self) -> &str { + "dataframe" + } + + fn usage(&self) -> &str { + "Dataframe commands" + } + + fn signature(&self) -> Signature { + Signature::build(self.name()).category(Category::Custom("dataframe".into())) + } + + fn run( + &self, + engine_state: &EngineState, + _stack: &mut Stack, + call: &Call, + _input: PipelineData, + ) -> Result { + Ok(Value::String { + val: get_full_help(&Dataframe.signature(), &Dataframe.examples(), engine_state), + span: call.head, + } + .into_pipeline_data()) + } +} diff --git a/crates/nu-command/src/dataframe/describe.rs b/crates/nu-command/src/dataframe/describe.rs index 80bf41582..93a4458bc 100644 --- a/crates/nu-command/src/dataframe/describe.rs +++ b/crates/nu-command/src/dataframe/describe.rs @@ -17,7 +17,7 @@ pub struct DescribeDF; impl Command for DescribeDF { fn name(&self) -> &str { - "describe-df" + "dataframe describe" } fn usage(&self) -> &str { @@ -31,7 +31,7 @@ impl Command for DescribeDF { fn examples(&self) -> Vec { vec![Example { description: "dataframe description", - example: "[[a b]; [1 1] [1 1]] | to df | describe-df", + example: "[[a b]; [1 1] [1 1]] | dataframe to-df | dataframe describe", result: Some( NuDataFrame::try_from_columns(vec![ Column::new( diff --git a/crates/nu-command/src/dataframe/drop.rs b/crates/nu-command/src/dataframe/drop.rs index 7be2361ce..c632d0f0a 100644 --- a/crates/nu-command/src/dataframe/drop.rs +++ b/crates/nu-command/src/dataframe/drop.rs @@ -13,7 +13,7 @@ pub struct DropDF; impl Command for DropDF { fn name(&self) -> &str { - "drop-df" + "dataframe drop" } fn usage(&self) -> &str { @@ -29,7 +29,7 @@ impl Command for DropDF { fn examples(&self) -> Vec { vec![Example { description: "drop column a", - example: "[[a b]; [1 2] [3 4]] | to df | drop-df a", + example: "[[a b]; [1 2] [3 4]] | dataframe to-df | dataframe drop a", result: Some( NuDataFrame::try_from_columns(vec![Column::new( "b".to_string(), diff --git a/crates/nu-command/src/dataframe/dtypes.rs b/crates/nu-command/src/dataframe/dtypes.rs index 3152f0d91..56da61bf2 100644 --- a/crates/nu-command/src/dataframe/dtypes.rs +++ b/crates/nu-command/src/dataframe/dtypes.rs @@ -10,7 +10,7 @@ pub struct DataTypes; impl Command for DataTypes { fn name(&self) -> &str { - "dtypes" + "dataframe dtypes" } fn usage(&self) -> &str { @@ -24,7 +24,7 @@ impl Command for DataTypes { fn examples(&self) -> Vec { vec![Example { description: "Dataframe dtypes", - example: "[[a b]; [1 2] [3 4]] | to df | dtypes", + example: "[[a b]; [1 2] [3 4]] | dataframe to-df | dataframe dtypes", result: Some( NuDataFrame::try_from_columns(vec![ Column::new( diff --git a/crates/nu-command/src/dataframe/mod.rs b/crates/nu-command/src/dataframe/mod.rs index beab822b7..20fe6bcc5 100644 --- a/crates/nu-command/src/dataframe/mod.rs +++ b/crates/nu-command/src/dataframe/mod.rs @@ -1,7 +1,9 @@ +mod series; mod values; mod append; mod column; +mod command; mod describe; mod drop; mod dtypes; @@ -10,6 +12,7 @@ mod to_df; pub use append::AppendDF; pub use column::ColumnDF; +pub use command::Dataframe; pub use describe::DescribeDF; pub use drop::DropDF; pub use dtypes::DataTypes; @@ -31,6 +34,7 @@ pub fn add_dataframe_decls(working_set: &mut StateWorkingSet) { bind_command!( AppendDF, ColumnDF, + Dataframe, DataTypes, DescribeDF, DropDF, diff --git a/crates/nu-command/src/dataframe/open.rs b/crates/nu-command/src/dataframe/open.rs index 19fb85f9d..7c82f101c 100644 --- a/crates/nu-command/src/dataframe/open.rs +++ b/crates/nu-command/src/dataframe/open.rs @@ -14,7 +14,7 @@ pub struct OpenDataFrame; impl Command for OpenDataFrame { fn name(&self) -> &str { - "open-df" + "dataframe open" } fn usage(&self) -> &str { @@ -63,7 +63,7 @@ impl Command for OpenDataFrame { fn examples(&self) -> Vec { vec![Example { description: "Takes a file name and creates a dataframe", - example: "open-df test.csv", + example: "dataframe open test.csv", result: None, }] } diff --git a/crates/nu-command/src/dataframe/series/mod.rs b/crates/nu-command/src/dataframe/series/mod.rs new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/crates/nu-command/src/dataframe/series/mod.rs @@ -0,0 +1 @@ + diff --git a/crates/nu-command/src/dataframe/to_df.rs b/crates/nu-command/src/dataframe/to_df.rs index 603d56fcb..308d61a72 100644 --- a/crates/nu-command/src/dataframe/to_df.rs +++ b/crates/nu-command/src/dataframe/to_df.rs @@ -11,7 +11,7 @@ pub struct ToDataFrame; impl Command for ToDataFrame { fn name(&self) -> &str { - "to df" + "dataframe to-df" } fn usage(&self) -> &str { @@ -26,7 +26,7 @@ impl Command for ToDataFrame { vec![ Example { description: "Takes a dictionary and creates a dataframe", - example: "[[a b];[1 2] [3 4]] | to df", + example: "[[a b];[1 2] [3 4]] | dataframe to-df", result: Some( NuDataFrame::try_from_columns(vec![ Column::new("a".to_string(), vec![1.into(), 3.into()]), @@ -38,7 +38,7 @@ impl Command for ToDataFrame { }, Example { description: "Takes a list of tables and creates a dataframe", - example: "[[1 2 a] [3 4 b] [5 6 c]] | to df", + example: "[[1 2 a] [3 4 b] [5 6 c]] | dataframe to-df", result: Some( NuDataFrame::try_from_columns(vec![ Column::new("0".to_string(), vec![1.into(), 3.into(), 5.into()]), @@ -58,7 +58,7 @@ impl Command for ToDataFrame { }, Example { description: "Takes a list and creates a dataframe", - example: "[a b c] | to df", + example: "[a b c] | dataframe to-df", result: Some( NuDataFrame::try_from_columns(vec![Column::new( "0".to_string(), @@ -74,7 +74,7 @@ impl Command for ToDataFrame { }, Example { description: "Takes a list of booleans and creates a dataframe", - example: "[$true $true $false] | to df", + example: "[$true $true $false] | dataframe to-df", result: Some( NuDataFrame::try_from_columns(vec![Column::new( "0".to_string(), diff --git a/crates/nu-engine/src/documentation.rs b/crates/nu-engine/src/documentation.rs index 4214340aa..ed4211e29 100644 --- a/crates/nu-engine/src/documentation.rs +++ b/crates/nu-engine/src/documentation.rs @@ -273,7 +273,7 @@ fn get_flags_section(signature: &Signature) -> String { if let Some(short) = flag.short { if flag.required { format!( - " -{}{} (required parameter){:?} {}\n", + " -{}{} (required parameter) {:?} {}\n", short, if !flag.long.is_empty() { format!(", --{}", flag.long) @@ -298,7 +298,7 @@ fn get_flags_section(signature: &Signature) -> String { } } else if flag.required { format!( - " --{} (required parameter){:?} {}\n", + " --{} (required parameter) {:?} {}\n", flag.long, arg, flag.desc ) } else {