mirror of
https://github.com/nushell/nushell.git
synced 2025-08-18 18:28:25 +02:00
Standardise to commands (#5800)
* standarize to commands * move from to to into
This commit is contained in:
@@ -30,7 +30,7 @@ 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);
|
||||
example: r#"let a = ([[a b]; [1 2] [3 4]] | into df);
|
||||
$a | append $a"#,
|
||||
result: Some(
|
||||
NuDataFrame::try_from_columns(vec![
|
||||
@@ -57,7 +57,7 @@ impl Command for AppendDF {
|
||||
},
|
||||
Example {
|
||||
description: "Appends a dataframe merging at the end of columns",
|
||||
example: r#"let a = ([[a b]; [1 2] [3 4]] | to-df);
|
||||
example: r#"let a = ([[a b]; [1 2] [3 4]] | into df);
|
||||
$a | append $a --col"#,
|
||||
result: Some(
|
||||
NuDataFrame::try_from_columns(vec![
|
||||
|
@@ -40,7 +40,7 @@ impl Command for DescribeDF {
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
description: "dataframe description",
|
||||
example: "[[a b]; [1 1] [1 1]] | to-df | describe",
|
||||
example: "[[a b]; [1 1] [1 1]] | into df | describe",
|
||||
result: Some(
|
||||
NuDataFrame::try_from_columns(vec![
|
||||
Column::new(
|
||||
|
@@ -29,7 +29,7 @@ impl Command for DropDF {
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
description: "drop column a",
|
||||
example: "[[a b]; [1 2] [3 4]] | to-df | drop a",
|
||||
example: "[[a b]; [1 2] [3 4]] | into df | drop a",
|
||||
result: Some(
|
||||
NuDataFrame::try_from_columns(vec![Column::new(
|
||||
"b".to_string(),
|
||||
|
@@ -40,7 +40,7 @@ impl Command for DropDuplicates {
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
description: "drop duplicates",
|
||||
example: "[[a b]; [1 2] [3 4] [1 2]] | to-df | drop-duplicates",
|
||||
example: "[[a b]; [1 2] [3 4] [1 2]] | into df | drop-duplicates",
|
||||
result: Some(
|
||||
NuDataFrame::try_from_columns(vec![
|
||||
Column::new(
|
||||
|
@@ -34,7 +34,7 @@ impl Command for DropNulls {
|
||||
vec![
|
||||
Example {
|
||||
description: "drop null values in dataframe",
|
||||
example: r#"let df = ([[a b]; [1 2] [3 0] [1 2]] | to-df);
|
||||
example: r#"let df = ([[a b]; [1 2] [3 0] [1 2]] | into df);
|
||||
let res = ($df.b / $df.b);
|
||||
let a = ($df | with-column $res --name res);
|
||||
$a | drop-nulls"#,
|
||||
@@ -59,7 +59,7 @@ impl Command for DropNulls {
|
||||
},
|
||||
Example {
|
||||
description: "drop null values in dataframe",
|
||||
example: r#"let s = ([1 2 0 0 3 4] | to-df);
|
||||
example: r#"let s = ([1 2 0 0 3 4] | into df);
|
||||
($s / $s) | drop-nulls"#,
|
||||
result: Some(
|
||||
NuDataFrame::try_from_columns(vec![Column::new(
|
||||
|
@@ -24,7 +24,7 @@ impl Command for DataTypes {
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
description: "Dataframe dtypes",
|
||||
example: "[[a b]; [1 2] [3 4]] | to-df | dtypes",
|
||||
example: "[[a b]; [1 2] [3 4]] | into df | dtypes",
|
||||
result: Some(
|
||||
NuDataFrame::try_from_columns(vec![
|
||||
Column::new(
|
||||
|
@@ -11,7 +11,7 @@ pub struct Dummies;
|
||||
|
||||
impl Command for Dummies {
|
||||
fn name(&self) -> &str {
|
||||
"to-dummies"
|
||||
"dummies"
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
@@ -26,7 +26,7 @@ impl Command for Dummies {
|
||||
vec![
|
||||
Example {
|
||||
description: "Create new dataframe with dummy variables from a dataframe",
|
||||
example: "[[a b]; [1 2] [3 4]] | to-df | to-dummies",
|
||||
example: "[[a b]; [1 2] [3 4]] | into df | dummies",
|
||||
result: Some(
|
||||
NuDataFrame::try_from_columns(vec![
|
||||
Column::new(
|
||||
@@ -52,7 +52,7 @@ impl Command for Dummies {
|
||||
},
|
||||
Example {
|
||||
description: "Create new dataframe with dummy variables from a series",
|
||||
example: "[1 2 2 3 3] | to-df | to-dummies",
|
||||
example: "[1 2 2 3 3] | into df | dummies",
|
||||
result: Some(
|
||||
NuDataFrame::try_from_columns(vec![
|
||||
Column::new(
|
||||
|
@@ -36,8 +36,8 @@ impl Command for FilterWith {
|
||||
vec![
|
||||
Example {
|
||||
description: "Filter dataframe using a bool mask",
|
||||
example: r#"let mask = ([true false] | to-df);
|
||||
[[a b]; [1 2] [3 4]] | to-df | filter-with $mask"#,
|
||||
example: r#"let mask = ([true false] | into df);
|
||||
[[a b]; [1 2] [3 4]] | into df | filter-with $mask"#,
|
||||
result: Some(
|
||||
NuDataFrame::try_from_columns(vec![
|
||||
Column::new("a".to_string(), vec![Value::test_int(1)]),
|
||||
@@ -49,7 +49,7 @@ impl Command for FilterWith {
|
||||
},
|
||||
Example {
|
||||
description: "Filter dataframe using an expression",
|
||||
example: "[[a b]; [1 2] [3 4]] | to-df | filter-with ((col a) > 1)",
|
||||
example: "[[a b]; [1 2] [3 4]] | into df | filter-with ((col a) > 1)",
|
||||
result: Some(
|
||||
NuDataFrame::try_from_columns(vec![
|
||||
Column::new("a".to_string(), vec![Value::test_int(3)]),
|
||||
|
@@ -27,7 +27,7 @@ impl Command for FirstDF {
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
description: "Create new dataframe with head rows",
|
||||
example: "[[a b]; [1 2] [3 4]] | to-df | first 1",
|
||||
example: "[[a b]; [1 2] [3 4]] | into df | first 1",
|
||||
result: Some(
|
||||
NuDataFrame::try_from_columns(vec![
|
||||
Column::new("a".to_string(), vec![Value::test_int(1)]),
|
||||
|
@@ -30,7 +30,7 @@ impl Command for GetDF {
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
description: "Returns the selected column",
|
||||
example: "[[a b]; [1 2] [3 4]] | to-df | get a",
|
||||
example: "[[a b]; [1 2] [3 4]] | into df | get a",
|
||||
result: Some(
|
||||
NuDataFrame::try_from_columns(vec![Column::new(
|
||||
"a".to_string(),
|
||||
|
@@ -27,7 +27,7 @@ impl Command for LastDF {
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
description: "Create new dataframe with last rows",
|
||||
example: "[[a b]; [1 2] [3 4]] | to-df | last 1",
|
||||
example: "[[a b]; [1 2] [3 4]] | into df | last 1",
|
||||
result: Some(
|
||||
NuDataFrame::try_from_columns(vec![
|
||||
Column::new("a".to_string(), vec![Value::test_int(3)]),
|
||||
|
@@ -25,7 +25,7 @@ impl Command for ListDF {
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
description: "Creates a new dataframe and shows it in the dataframe list",
|
||||
example: r#"let test = ([[a b];[1 2] [3 4]] | to-df);
|
||||
example: r#"let test = ([[a b];[1 2] [3 4]] | into df);
|
||||
ls-df"#,
|
||||
result: None,
|
||||
}]
|
||||
|
@@ -54,7 +54,8 @@ impl Command for MeltDF {
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
description: "melt dataframe",
|
||||
example: "[[a b c d]; [x 1 4 a] [y 2 5 b] [z 3 6 c]] | to-df | melt -c [b c] -v [a d]",
|
||||
example:
|
||||
"[[a b c d]; [x 1 4 a] [y 2 5 b] [z 3 6 c]] | into df | melt -c [b c] -v [a d]",
|
||||
result: Some(
|
||||
NuDataFrame::try_from_columns(vec![
|
||||
Column::new(
|
||||
|
@@ -40,7 +40,7 @@ impl Command for RenameDF {
|
||||
vec![
|
||||
Example {
|
||||
description: "Renames a series",
|
||||
example: "[5 6 7 8] | to-df | rename '0' new_name",
|
||||
example: "[5 6 7 8] | into df | rename '0' new_name",
|
||||
result: Some(
|
||||
NuDataFrame::try_from_columns(vec![Column::new(
|
||||
"new_name".to_string(),
|
||||
@@ -57,7 +57,7 @@ impl Command for RenameDF {
|
||||
},
|
||||
Example {
|
||||
description: "Renames a dataframe column",
|
||||
example: "[[a b]; [1 2] [3 4]] | to-df | rename a a_new",
|
||||
example: "[[a b]; [1 2] [3 4]] | into df | rename a a_new",
|
||||
result: Some(
|
||||
NuDataFrame::try_from_columns(vec![
|
||||
Column::new(
|
||||
@@ -75,7 +75,7 @@ impl Command for RenameDF {
|
||||
},
|
||||
Example {
|
||||
description: "Renames two dataframe columns",
|
||||
example: "[[a b]; [1 2] [3 4]] | to-df | rename [a b] [a_new b_new]",
|
||||
example: "[[a b]; [1 2] [3 4]] | into df | rename [a b] [a_new b_new]",
|
||||
result: Some(
|
||||
NuDataFrame::try_from_columns(vec![
|
||||
Column::new(
|
||||
|
@@ -48,12 +48,12 @@ impl Command for SampleDF {
|
||||
vec![
|
||||
Example {
|
||||
description: "Sample rows from dataframe",
|
||||
example: "[[a b]; [1 2] [3 4]] | to-df | sample -n 1",
|
||||
example: "[[a b]; [1 2] [3 4]] | into df | sample -n 1",
|
||||
result: None, // No expected value because sampling is random
|
||||
},
|
||||
Example {
|
||||
description: "Shows sample row using fraction and replace",
|
||||
example: "[[a b]; [1 2] [3 4] [5 6]] | to-df | sample -f 0.5 -e",
|
||||
example: "[[a b]; [1 2] [3 4] [5 6]] | into df | sample -f 0.5 -e",
|
||||
result: None, // No expected value because sampling is random
|
||||
},
|
||||
]
|
||||
|
@@ -27,7 +27,7 @@ impl Command for ShapeDF {
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
description: "Shows row and column shape",
|
||||
example: "[[a b]; [1 2] [3 4]] | to-df | shape",
|
||||
example: "[[a b]; [1 2] [3 4]] | into df | shape",
|
||||
result: Some(
|
||||
NuDataFrame::try_from_columns(vec![
|
||||
Column::new("rows".to_string(), vec![Value::test_int(2)]),
|
||||
|
@@ -31,7 +31,7 @@ impl Command for SliceDF {
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
description: "Create new dataframe from a slice of the rows",
|
||||
example: "[[a b]; [1 2] [3 4]] | to-df | slice 0 1",
|
||||
example: "[[a b]; [1 2] [3 4]] | into df | slice 0 1",
|
||||
result: Some(
|
||||
NuDataFrame::try_from_columns(vec![
|
||||
Column::new("a".to_string(), vec![Value::test_int(1)]),
|
||||
|
@@ -36,8 +36,8 @@ impl Command for TakeDF {
|
||||
vec![
|
||||
Example {
|
||||
description: "Takes selected rows from dataframe",
|
||||
example: r#"let df = ([[a b]; [4 1] [5 2] [4 3]] | to-df);
|
||||
let indices = ([0 2] | to-df);
|
||||
example: r#"let df = ([[a b]; [4 1] [5 2] [4 3]] | into df);
|
||||
let indices = ([0 2] | into df);
|
||||
$df | take $indices"#,
|
||||
result: Some(
|
||||
NuDataFrame::try_from_columns(vec![
|
||||
@@ -56,8 +56,8 @@ impl Command for TakeDF {
|
||||
},
|
||||
Example {
|
||||
description: "Takes selected rows from series",
|
||||
example: r#"let series = ([4 1 5 2 4 3] | to-df);
|
||||
let indices = ([0 2] | to-df);
|
||||
example: r#"let series = ([4 1 5 2 4 3] | into df);
|
||||
let indices = ([0 2] | into df);
|
||||
$series | take $indices"#,
|
||||
result: Some(
|
||||
NuDataFrame::try_from_columns(vec![Column::new(
|
||||
|
@@ -15,7 +15,7 @@ pub struct ToCSV;
|
||||
|
||||
impl Command for ToCSV {
|
||||
fn name(&self) -> &str {
|
||||
"to-csv"
|
||||
"to csv"
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
@@ -39,12 +39,12 @@ impl Command for ToCSV {
|
||||
vec![
|
||||
Example {
|
||||
description: "Saves dataframe to csv file",
|
||||
example: "[[a b]; [1 2] [3 4]] | to-df | to-csv test.csv",
|
||||
example: "[[a b]; [1 2] [3 4]] | into df | to csv test.csv",
|
||||
result: None,
|
||||
},
|
||||
Example {
|
||||
description: "Saves dataframe to csv file using other delimiter",
|
||||
example: "[[a b]; [1 2] [3 4]] | to-df | to-csv test.csv -d '|'",
|
||||
example: "[[a b]; [1 2] [3 4]] | into df | to csv test.csv -d '|'",
|
||||
result: None,
|
||||
},
|
||||
]
|
||||
|
@@ -11,7 +11,7 @@ pub struct ToDataFrame;
|
||||
|
||||
impl Command for ToDataFrame {
|
||||
fn name(&self) -> &str {
|
||||
"to-df"
|
||||
"into 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]] | into df",
|
||||
result: Some(
|
||||
NuDataFrame::try_from_columns(vec![
|
||||
Column::new(
|
||||
@@ -44,7 +44,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]] | into df",
|
||||
result: Some(
|
||||
NuDataFrame::try_from_columns(vec![
|
||||
Column::new(
|
||||
@@ -70,7 +70,7 @@ impl Command for ToDataFrame {
|
||||
},
|
||||
Example {
|
||||
description: "Takes a list and creates a dataframe",
|
||||
example: "[a b c] | to-df",
|
||||
example: "[a b c] | into df",
|
||||
result: Some(
|
||||
NuDataFrame::try_from_columns(vec![Column::new(
|
||||
"0".to_string(),
|
||||
@@ -86,7 +86,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] | into df",
|
||||
result: Some(
|
||||
NuDataFrame::try_from_columns(vec![Column::new(
|
||||
"0".to_string(),
|
||||
|
@@ -2,7 +2,7 @@ use nu_engine::CallExt;
|
||||
use nu_protocol::{
|
||||
ast::Call,
|
||||
engine::{Command, EngineState, Stack},
|
||||
Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
|
||||
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
|
||||
};
|
||||
|
||||
use super::super::values::NuDataFrame;
|
||||
@@ -12,11 +12,11 @@ pub struct ToNu;
|
||||
|
||||
impl Command for ToNu {
|
||||
fn name(&self) -> &str {
|
||||
"to-nu"
|
||||
"into nu"
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Converts a section of the dataframe to Nushell Table"
|
||||
"Converts a section of the dataframe into nushell Table"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
@@ -32,16 +32,34 @@ impl Command for ToNu {
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
let cols = vec!["a".into(), "b".into()];
|
||||
let rec_1 = Value::Record {
|
||||
cols: cols.clone(),
|
||||
vals: vec![Value::test_int(1), Value::test_int(2)],
|
||||
span: Span::test_data(),
|
||||
};
|
||||
let rec_2 = Value::Record {
|
||||
cols,
|
||||
vals: vec![Value::test_int(3), Value::test_int(4)],
|
||||
span: Span::test_data(),
|
||||
};
|
||||
|
||||
vec![
|
||||
Example {
|
||||
description: "Shows head rows from dataframe",
|
||||
example: "[[a b]; [1 2] [3 4]] | to-df | to nu",
|
||||
result: None,
|
||||
example: "[[a b]; [1 2] [3 4]] | into df | into nu",
|
||||
result: Some(Value::List {
|
||||
vals: vec![rec_1, rec_2.clone()],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
},
|
||||
Example {
|
||||
description: "Shows tail rows from dataframe",
|
||||
example: "[[a b]; [1 2] [3 4] [5 6]] | to-df | to nu -t -n 1",
|
||||
result: None,
|
||||
example: "[[a b]; [1 2] [5 6] [3 4]] | into df | into nu -t -n 1",
|
||||
result: Some(Value::List {
|
||||
vals: vec![rec_2],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
},
|
||||
]
|
||||
}
|
||||
@@ -71,7 +89,7 @@ fn command(
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let rows: Option<usize> = call.get_flag(engine_state, stack, "n-rows")?;
|
||||
let rows: Option<usize> = call.get_flag(engine_state, stack, "rows")?;
|
||||
let tail: bool = call.has_flag("tail");
|
||||
|
||||
let df = NuDataFrame::try_from_pipeline(input, call.head)?;
|
||||
@@ -87,6 +105,8 @@ fn command(
|
||||
}
|
||||
};
|
||||
|
||||
println!("len: {}", values.len());
|
||||
|
||||
let value = Value::List {
|
||||
vals: values,
|
||||
span: call.head,
|
||||
@@ -94,3 +114,14 @@ fn command(
|
||||
|
||||
Ok(PipelineData::Value(value, None))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::super::super::test_dataframe::test_dataframe;
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_examples() {
|
||||
test_dataframe(vec![Box::new(ToNu {})])
|
||||
}
|
||||
}
|
||||
|
@@ -15,7 +15,7 @@ pub struct ToParquet;
|
||||
|
||||
impl Command for ToParquet {
|
||||
fn name(&self) -> &str {
|
||||
"to-parquet"
|
||||
"to parquet"
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
@@ -31,7 +31,7 @@ impl Command for ToParquet {
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
description: "Saves dataframe to parquet file",
|
||||
example: "[[a b]; [1 2] [3 4]] | to-df | to-parquet test.parquet",
|
||||
example: "[[a b]; [1 2] [3 4]] | into df | to parquet test.parquet",
|
||||
result: None,
|
||||
}]
|
||||
}
|
||||
|
@@ -35,8 +35,8 @@ impl Command for WithColumn {
|
||||
Example {
|
||||
description: "Adds a series to the dataframe",
|
||||
example: r#"[[a b]; [1 2] [3 4]]
|
||||
| to-df
|
||||
| with-column ([5 6] | to-df) --name c"#,
|
||||
| into df
|
||||
| with-column ([5 6] | into df) --name c"#,
|
||||
result: Some(
|
||||
NuDataFrame::try_from_columns(vec![
|
||||
Column::new(
|
||||
@@ -59,7 +59,7 @@ impl Command for WithColumn {
|
||||
Example {
|
||||
description: "Adds a series to the dataframe",
|
||||
example: r#"[[a b]; [1 2] [3 4]]
|
||||
| to-lazy
|
||||
| into lazy
|
||||
| with-column [
|
||||
((col a) * 2 | as "c")
|
||||
((col a) * 3 | as "d")
|
||||
|
Reference in New Issue
Block a user