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
This commit is contained in:
Fernando Herrera 2021-07-01 05:33:52 +01:00 committed by GitHub
parent bb5ab5d16c
commit 17008bb648
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 148 additions and 142 deletions

View File

@ -11,8 +11,6 @@ use polars::{
prelude::{DataType, PolarsError, Series}, prelude::{DataType, PolarsError, Series},
}; };
use super::utils::convert_columns;
enum Operation { enum Operation {
Mean, Mean,
Sum, Sum,
@ -90,11 +88,6 @@ impl WholeStreamCommand for DataFrame {
fn signature(&self) -> Signature { fn signature(&self) -> Signature {
Signature::build("dataframe aggregate") Signature::build("dataframe aggregate")
.required("operation", SyntaxShape::String, "aggregate operation") .required("operation", SyntaxShape::String, "aggregate operation")
.optional(
"selection",
SyntaxShape::Table,
"columns to perform aggregation",
)
.named( .named(
"quantile", "quantile",
SyntaxShape::Number, SyntaxShape::Number,
@ -117,7 +110,7 @@ impl WholeStreamCommand for DataFrame {
Example { Example {
description: "Aggregate sum by grouping by column a and summing on col b", description: "Aggregate sum by grouping by column a and summing on col b",
example: 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, result: None,
}, },
Example { Example {
@ -141,16 +134,6 @@ fn command(mut args: CommandArgs) -> Result<OutputStream, ShellError> {
let operation: Tagged<String> = args.req(0)?; let operation: Tagged<String> = args.req(0)?;
let op = Operation::from_tagged(&operation, quantile)?; let op = Operation::from_tagged(&operation, quantile)?;
// Extracting the selection columns of the columns to perform the aggregation
let agg_cols: Option<Vec<Value>> = 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(|| { let value = args.input.next().ok_or_else(|| {
ShellError::labeled_error("Empty stream", "No value found in the stream", &tag) ShellError::labeled_error("Empty stream", "No value found in the stream", &tag)
})?; })?;
@ -159,16 +142,11 @@ fn command(mut args: CommandArgs) -> Result<OutputStream, ShellError> {
UntaggedValue::DataFrame(PolarsData::GroupBy(nu_groupby)) => { UntaggedValue::DataFrame(PolarsData::GroupBy(nu_groupby)) => {
let groupby = nu_groupby.to_groupby()?; let groupby = nu_groupby.to_groupby()?;
let groupby = match &selection {
Some(cols) => groupby.select(cols),
None => groupby,
};
let res = perform_groupby_aggregation( let res = perform_groupby_aggregation(
groupby, groupby,
op, op,
&operation.tag, &operation.tag,
&agg_span, &tag.span,
args.has_flag("explicit"), args.has_flag("explicit"),
)?; )?;
@ -177,16 +155,7 @@ fn command(mut args: CommandArgs) -> Result<OutputStream, ShellError> {
UntaggedValue::DataFrame(PolarsData::EagerDataFrame(df)) => { UntaggedValue::DataFrame(PolarsData::EagerDataFrame(df)) => {
let df = df.as_ref(); let df = df.as_ref();
let res = match &selection { let res = perform_dataframe_aggregation(&df, op, &operation.tag)?;
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),
}?;
Ok(OutputStream::one(NuDataFrame::dataframe_to_value(res, tag))) Ok(OutputStream::one(NuDataFrame::dataframe_to_value(res, tag)))
} }

View File

@ -17,11 +17,7 @@ impl WholeStreamCommand for DataFrame {
} }
fn signature(&self) -> Signature { fn signature(&self) -> Signature {
Signature::build("dataframe drop").required( Signature::build("dataframe drop").rest(SyntaxShape::Any, "column names to be dropped")
"columns",
SyntaxShape::Table,
"column names to be dropped",
)
} }
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> { fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
@ -31,7 +27,7 @@ impl WholeStreamCommand for DataFrame {
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
vec![Example { vec![Example {
description: "drop column a", 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, result: None,
}] }]
} }
@ -40,7 +36,7 @@ impl WholeStreamCommand for DataFrame {
fn command(mut args: CommandArgs) -> Result<OutputStream, ShellError> { fn command(mut args: CommandArgs) -> Result<OutputStream, ShellError> {
let tag = args.call_info.name_tag.clone(); let tag = args.call_info.name_tag.clone();
let columns: Vec<Value> = args.req(0)?; let columns: Vec<Value> = args.rest(0)?;
let (col_string, col_span) = convert_columns(&columns, &tag)?; let (col_string, col_span) = convert_columns(&columns, &tag)?;
let df = NuDataFrame::try_from_stream(&mut args.input, &tag.span)?; let df = NuDataFrame::try_from_stream(&mut args.input, &tag.span)?;

View File

@ -36,16 +36,16 @@ impl WholeStreamCommand for DataFrame {
Example { Example {
description: "drop null values in dataframe", description: "drop null values in dataframe",
example: r#"let df = ([[a b]; [1 2] [3 0] [1 2]] | dataframe to-df); example: r#"let df = ([[a b]; [1 2] [3 0] [1 2]] | dataframe to-df);
let res = ($df.b / $df.b); let res = ($df.b / $df.b);
let df = ($df | dataframe with-column $res --name res); let df = ($df | dataframe with-column $res --name res);
$df | dataframe drop-nulls $df | dataframe drop-nulls
"#, "#,
result: None, result: None,
}, },
Example { Example {
description: "drop null values in dataframe", description: "drop null values in dataframe",
example: r#"let s = ([1 2 0 0 3 4] | dataframe to-series); 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, result: None,
}, },
] ]

View File

@ -35,13 +35,13 @@ impl WholeStreamCommand for DataFrame {
Example { Example {
description: "Filter dataframe using a bool mask", description: "Filter dataframe using a bool mask",
example: r#"let mask = ([$true $false] | dataframe to-series); 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, result: None,
}, },
Example { Example {
description: "Filter dataframe by creating a mask from operation", description: "Filter dataframe by creating a mask from operation",
example: r#"let mask = (([5 6] | dataframe to-series) > 5); 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, result: None,
}, },
] ]

View File

@ -9,11 +9,11 @@ pub struct DataFrame;
impl WholeStreamCommand for DataFrame { impl WholeStreamCommand for DataFrame {
fn name(&self) -> &str { fn name(&self) -> &str {
"dataframe head" "dataframe first"
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
"[DataFrame] Creates new dataframe with head rows" "[DataFrame] Creates new dataframe with first rows"
} }
fn signature(&self) -> Signature { fn signature(&self) -> Signature {
@ -31,7 +31,7 @@ impl WholeStreamCommand for DataFrame {
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
vec![Example { vec![Example {
description: "Create new dataframe with head rows", 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, result: None,
}] }]
} }

View File

@ -16,11 +16,7 @@ impl WholeStreamCommand for DataFrame {
} }
fn signature(&self) -> Signature { fn signature(&self) -> Signature {
Signature::build("dataframe get").required( Signature::build("dataframe get").rest(SyntaxShape::Any, "column names to sort dataframe")
"columns",
SyntaxShape::Table,
"column names to sort dataframe",
)
} }
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> { fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
@ -30,7 +26,7 @@ impl WholeStreamCommand for DataFrame {
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
vec![Example { vec![Example {
description: "Creates dataframe with selected columns", 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, result: None,
}] }]
} }
@ -38,7 +34,7 @@ impl WholeStreamCommand for DataFrame {
fn command(mut args: CommandArgs) -> Result<OutputStream, ShellError> { fn command(mut args: CommandArgs) -> Result<OutputStream, ShellError> {
let tag = args.call_info.name_tag.clone(); let tag = args.call_info.name_tag.clone();
let columns: Vec<Value> = args.req(0)?; let columns: Vec<Value> = args.rest(0)?;
let (col_string, col_span) = convert_columns(&columns, &tag)?; let (col_string, col_span) = convert_columns(&columns, &tag)?;

View File

@ -20,11 +20,7 @@ impl WholeStreamCommand for DataFrame {
} }
fn signature(&self) -> Signature { fn signature(&self) -> Signature {
Signature::build("dataframe group-by").required( Signature::build("dataframe group-by").rest(SyntaxShape::Any, "groupby columns")
"by columns",
SyntaxShape::Table,
"groupby columns",
)
} }
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> { fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
@ -34,7 +30,7 @@ impl WholeStreamCommand for DataFrame {
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
vec![Example { vec![Example {
description: "Grouping by column a", 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, result: None,
}] }]
} }
@ -44,7 +40,7 @@ fn command(mut args: CommandArgs) -> Result<OutputStream, ShellError> {
let tag = args.call_info.name_tag.clone(); let tag = args.call_info.name_tag.clone();
// Extracting the names of the columns to perform the groupby // Extracting the names of the columns to perform the groupby
let by_columns: Vec<Value> = args.req(0)?; let by_columns: Vec<Value> = args.rest(0)?;
let (columns_string, col_span) = convert_columns(&by_columns, &tag)?; let (columns_string, col_span) = convert_columns(&by_columns, &tag)?;
let df = NuDataFrame::try_from_stream(&mut args.input, &tag.span)?; let df = NuDataFrame::try_from_stream(&mut args.input, &tag.span)?;

View File

@ -26,15 +26,17 @@ impl WholeStreamCommand for DataFrame {
fn signature(&self) -> Signature { fn signature(&self) -> Signature {
Signature::build("dataframe join") Signature::build("dataframe join")
.required("dataframe", SyntaxShape::Any, "right dataframe to join") .required("dataframe", SyntaxShape::Any, "right dataframe to join")
.required( .required_named(
"l_columns", "left",
SyntaxShape::Table, SyntaxShape::Table,
"left column names to perform join", "left column names to perform join",
Some('l'),
) )
.required( .required_named(
"r_columns", "right",
SyntaxShape::Table, SyntaxShape::Table,
"right column names to perform join", "right column names to perform join",
Some('r'),
) )
.named( .named(
"type", "type",
@ -52,13 +54,14 @@ impl WholeStreamCommand for DataFrame {
vec![ vec![
Example { Example {
description: "inner join dataframe", 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, result: None,
}, },
Example { Example {
description: "right join dataframe", description: "right join dataframe",
example: example: r#"let right = ([[a b c]; [1 2 3] [3 4 5] [5 6 7]] | dataframe to-df);
"[[a b]; [1 2] [3 4] [5 6]] | dataframe to-df | dataframe join $right [b] [b] -t right", $right | dataframe join $right -l [a c] -r [a c] -t inner"#,
result: None, result: None,
}, },
] ]
@ -69,8 +72,8 @@ fn command(mut args: CommandArgs) -> Result<OutputStream, ShellError> {
let tag = args.call_info.name_tag.clone(); let tag = args.call_info.name_tag.clone();
let r_df: Value = args.req(0)?; let r_df: Value = args.req(0)?;
let l_col: Vec<Value> = args.req(1)?; let l_col: Vec<Value> = args.req_named("left")?;
let r_col: Vec<Value> = args.req(2)?; let r_col: Vec<Value> = args.req_named("right")?;
let join_type_op: Option<Tagged<String>> = args.get_flag("type")?; let join_type_op: Option<Tagged<String>> = args.get_flag("type")?;
let join_type = match join_type_op { let join_type = match join_type_op {

View File

@ -8,7 +8,7 @@ pub struct DataFrame;
impl WholeStreamCommand for DataFrame { impl WholeStreamCommand for DataFrame {
fn name(&self) -> &str { fn name(&self) -> &str {
"dataframe tail" "dataframe last"
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
@ -16,7 +16,7 @@ impl WholeStreamCommand for DataFrame {
} }
fn signature(&self) -> Signature { fn signature(&self) -> Signature {
Signature::build("dataframe tail").optional( Signature::build("dataframe last").optional(
"n_rows", "n_rows",
SyntaxShape::Number, SyntaxShape::Number,
"Number of rows for tail", "Number of rows for tail",
@ -29,8 +29,8 @@ impl WholeStreamCommand for DataFrame {
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
vec![Example { vec![Example {
description: "Create new dataframe with tail rows", description: "Create new dataframe with last rows",
example: "[[a b]; [1 2] [3 4]] | dataframe to-df | dataframe tail", example: "[[a b]; [1 2] [3 4]] | dataframe to-df | dataframe last",
result: None, result: None,
}] }]
} }

View File

@ -19,11 +19,7 @@ impl WholeStreamCommand for DataFrame {
fn signature(&self) -> Signature { fn signature(&self) -> Signature {
Signature::build("dataframe melt") Signature::build("dataframe melt")
.required("id_columns", SyntaxShape::Table, "Id columns for melting") .required("id_columns", SyntaxShape::Table, "Id columns for melting")
.required( .rest(SyntaxShape::Any, "columns used as value columns")
"value_columns",
SyntaxShape::Table,
"columns used as value columns",
)
} }
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> { fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
@ -33,7 +29,7 @@ impl WholeStreamCommand for DataFrame {
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
vec![Example { vec![Example {
description: "melt dataframe", 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, result: None,
}] }]
} }
@ -43,7 +39,7 @@ fn command(mut args: CommandArgs) -> Result<OutputStream, ShellError> {
let tag = args.call_info.name_tag.clone(); let tag = args.call_info.name_tag.clone();
let id_col: Vec<Value> = args.req(0)?; let id_col: Vec<Value> = args.req(0)?;
let val_col: Vec<Value> = args.req(1)?; let val_col: Vec<Value> = args.rest(1)?;
let (id_col_string, id_col_span) = convert_columns(&id_col, &tag)?; let (id_col_string, id_col_span) = convert_columns(&id_col, &tag)?;
let (val_col_string, val_col_span) = convert_columns(&val_col, &tag)?; let (val_col_string, val_col_span) = convert_columns(&val_col, &tag)?;

View File

@ -7,20 +7,20 @@ pub mod drop_nulls;
pub mod dtypes; pub mod dtypes;
pub mod dummies; pub mod dummies;
pub mod filter; pub mod filter;
pub mod first;
pub mod get; pub mod get;
pub mod groupby; pub mod groupby;
pub mod head;
pub mod join; pub mod join;
pub mod last;
pub mod list; pub mod list;
pub mod load;
pub mod melt; pub mod melt;
pub mod open;
pub mod pivot; pub mod pivot;
pub mod sample; pub mod sample;
pub mod select; pub mod select;
pub mod show; pub mod show;
pub mod slice; pub mod slice;
pub mod sort; pub mod sort;
pub mod tail;
pub mod to_csv; pub mod to_csv;
pub mod to_df; pub mod to_df;
pub mod to_parquet; pub mod to_parquet;
@ -38,20 +38,20 @@ pub use drop_nulls::DataFrame as DataFrameDropNulls;
pub use dtypes::DataFrame as DataFrameDTypes; pub use dtypes::DataFrame as DataFrameDTypes;
pub use dummies::DataFrame as DataFrameDummies; pub use dummies::DataFrame as DataFrameDummies;
pub use filter::DataFrame as DataFrameFilter; pub use filter::DataFrame as DataFrameFilter;
pub use first::DataFrame as DataFrameFirst;
pub use get::DataFrame as DataFrameGet; pub use get::DataFrame as DataFrameGet;
pub use groupby::DataFrame as DataFrameGroupBy; pub use groupby::DataFrame as DataFrameGroupBy;
pub use head::DataFrame as DataFrameHead;
pub use join::DataFrame as DataFrameJoin; pub use join::DataFrame as DataFrameJoin;
pub use last::DataFrame as DataFrameLast;
pub use list::DataFrame as DataFrameList; pub use list::DataFrame as DataFrameList;
pub use load::DataFrame as DataFrameLoad;
pub use melt::DataFrame as DataFrameMelt; pub use melt::DataFrame as DataFrameMelt;
pub use open::DataFrame as DataFrameOpen;
pub use pivot::DataFrame as DataFramePivot; pub use pivot::DataFrame as DataFramePivot;
pub use sample::DataFrame as DataFrameSample; pub use sample::DataFrame as DataFrameSample;
pub use select::DataFrame as DataFrameSelect; pub use select::DataFrame as DataFrameSelect;
pub use show::DataFrame as DataFrameShow; pub use show::DataFrame as DataFrameShow;
pub use slice::DataFrame as DataFrameSlice; pub use slice::DataFrame as DataFrameSlice;
pub use sort::DataFrame as DataFrameSort; pub use sort::DataFrame as DataFrameSort;
pub use tail::DataFrame as DataFrameTail;
pub use to_csv::DataFrame as DataFrameToCsv; pub use to_csv::DataFrame as DataFrameToCsv;
pub use to_df::DataFrame as DataFrameToDF; pub use to_df::DataFrame as DataFrameToDF;
pub use to_parquet::DataFrame as DataFrameToParquet; pub use to_parquet::DataFrame as DataFrameToParquet;
@ -74,6 +74,7 @@ pub use series::DataFrameIsNull;
pub use series::DataFrameIsUnique; pub use series::DataFrameIsUnique;
pub use series::DataFrameNNull; pub use series::DataFrameNNull;
pub use series::DataFrameNUnique; pub use series::DataFrameNUnique;
pub use series::DataFrameNot;
pub use series::DataFrameSeriesRename; pub use series::DataFrameSeriesRename;
pub use series::DataFrameSet; pub use series::DataFrameSet;
pub use series::DataFrameShift; pub use series::DataFrameShift;

View File

@ -15,15 +15,15 @@ pub struct DataFrame;
impl WholeStreamCommand for DataFrame { impl WholeStreamCommand for DataFrame {
fn name(&self) -> &str { fn name(&self) -> &str {
"dataframe load" "dataframe open"
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
"Loads dataframe form csv file" "Opens csv, json or parquet file to create dataframe"
} }
fn signature(&self) -> Signature { fn signature(&self) -> Signature {
Signature::build("dataframe load") Signature::build("dataframe open")
.required( .required(
"file", "file",
SyntaxShape::FilePath, SyntaxShape::FilePath,
@ -67,7 +67,7 @@ impl WholeStreamCommand for DataFrame {
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
vec![Example { vec![Example {
description: "Takes a file name and creates a dataframe", description: "Takes a file name and creates a dataframe",
example: "dataframe load test.csv", example: "dataframe open test.csv",
result: None, result: None,
}] }]
} }

View File

@ -72,7 +72,7 @@ impl WholeStreamCommand for DataFrame {
vec![Example { vec![Example {
description: "Pivot a dataframe on b and aggregation on col c", description: "Pivot a dataframe on b and aggregation on col c",
example: 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, result: None,
}] }]
} }

View File

@ -17,7 +17,7 @@ impl WholeStreamCommand for DataFrame {
} }
fn signature(&self) -> Signature { fn signature(&self) -> Signature {
Signature::build("dataframe load") Signature::build("dataframe sample")
.named( .named(
"n_rows", "n_rows",
SyntaxShape::Number, SyntaxShape::Number,

View File

@ -17,11 +17,7 @@ impl WholeStreamCommand for DataFrame {
} }
fn signature(&self) -> Signature { fn signature(&self) -> Signature {
Signature::build("dataframe select").required( Signature::build("dataframe select").rest(SyntaxShape::Any, "selected column names")
"columns",
SyntaxShape::Table,
"selected column names",
)
} }
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> { fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
@ -31,7 +27,7 @@ impl WholeStreamCommand for DataFrame {
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
vec![Example { vec![Example {
description: "Create new dataframe with column a", 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, result: None,
}] }]
} }
@ -40,7 +36,7 @@ impl WholeStreamCommand for DataFrame {
fn command(mut args: CommandArgs) -> Result<OutputStream, ShellError> { fn command(mut args: CommandArgs) -> Result<OutputStream, ShellError> {
let tag = args.call_info.name_tag.clone(); let tag = args.call_info.name_tag.clone();
let columns: Vec<Value> = args.req(0)?; let columns: Vec<Value> = args.rest(0)?;
let (col_string, col_span) = convert_columns(&columns, &tag)?; let (col_string, col_span) = convert_columns(&columns, &tag)?;

View File

@ -32,8 +32,8 @@ impl WholeStreamCommand for DataFrame {
Example { Example {
description: "Checks the result from a comparison", description: "Checks the result from a comparison",
example: r#"let s = ([5 6 2 8] | dataframe to-series); example: r#"let s = ([5 6 2 8] | dataframe to-series);
let res = ($s > 9); let res = ($s > 9);
$res | dataframe all-false"#, $res | dataframe all-false"#,
result: None, result: None,
}, },
] ]

View File

@ -32,8 +32,8 @@ impl WholeStreamCommand for DataFrame {
Example { Example {
description: "Checks the result from a comparison", description: "Checks the result from a comparison",
example: r#"let s = ([5 6 2 8] | dataframe to-series); example: r#"let s = ([5 6 2 8] | dataframe to-series);
let res = ($s > 9); let res = ($s > 9);
$res | dataframe all-true"#, $res | dataframe all-true"#,
result: None, result: None,
}, },
] ]

View File

@ -30,7 +30,7 @@ impl WholeStreamCommand for DataFrame {
vec![Example { vec![Example {
description: "Checks if elements from a series are contained in right series", description: "Checks if elements from a series are contained in right series",
example: r#"let other = ([1 3 6] | dataframe to-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, result: None,
}] }]
} }

View File

@ -27,8 +27,8 @@ impl WholeStreamCommand for DataFrame {
vec![Example { vec![Example {
description: "Create mask where values are not null", description: "Create mask where values are not null",
example: r#"let s = ([5 6 0 8] | dataframe to-series); example: r#"let s = ([5 6 0 8] | dataframe to-series);
let res = ($s / $s); let res = ($s / $s);
$res | dataframe is-not-null"#, $res | dataframe is-not-null"#,
result: None, result: None,
}] }]
} }

View File

@ -27,8 +27,8 @@ impl WholeStreamCommand for DataFrame {
vec![Example { vec![Example {
description: "Create mask where values are null", description: "Create mask where values are null",
example: r#"let s = ([5 6 0 8] | dataframe to-series); example: r#"let s = ([5 6 0 8] | dataframe to-series);
let res = ($s / $s); let res = ($s / $s);
$res | dataframe is-null"#, $res | dataframe is-null"#,
result: None, result: None,
}] }]
} }

View File

@ -12,6 +12,7 @@ pub mod is_null;
pub mod is_unique; pub mod is_unique;
pub mod n_null; pub mod n_null;
pub mod n_unique; pub mod n_unique;
pub mod not;
pub mod rename; pub mod rename;
pub mod set; pub mod set;
pub mod shift; pub mod shift;
@ -32,6 +33,7 @@ pub use is_null::DataFrame as DataFrameIsNull;
pub use is_unique::DataFrame as DataFrameIsUnique; pub use is_unique::DataFrame as DataFrameIsUnique;
pub use n_null::DataFrame as DataFrameNNull; pub use n_null::DataFrame as DataFrameNNull;
pub use n_unique::DataFrame as DataFrameNUnique; pub use n_unique::DataFrame as DataFrameNUnique;
pub use not::DataFrame as DataFrameNot;
pub use rename::DataFrame as DataFrameSeriesRename; pub use rename::DataFrame as DataFrameSeriesRename;
pub use set::DataFrame as DataFrameSet; pub use set::DataFrame as DataFrameSet;
pub use shift::DataFrame as DataFrameShift; pub use shift::DataFrame as DataFrameShift;

View File

@ -28,7 +28,7 @@ impl WholeStreamCommand for DataFrame {
vec![Example { vec![Example {
description: "Counts null values", description: "Counts null values",
example: r#"let s = ([1 1 0 0 3 3 4] | dataframe to-series); 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, result: None,
}] }]
} }

View File

@ -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<OutputStream, ShellError> {
command(args)
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Inverts boolean mask",
example: "[$true $false $true] | dataframe to-series | dataframe not",
result: None,
}]
}
}
fn command(mut args: CommandArgs) -> Result<OutputStream, ShellError> {
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,
)))
}

View File

@ -34,8 +34,8 @@ impl WholeStreamCommand for DataFrame {
vec![Example { vec![Example {
description: "Shifts the values by a given period", description: "Shifts the values by a given period",
example: r#"let s = ([1 2 2 3 3] | dataframe to-series | dataframe shift 2); example: r#"let s = ([1 2 2 3 3] | dataframe to-series | dataframe shift 2);
let mask = ($s | dataframe is-null); let mask = ($s | dataframe is-null);
$s | dataframe set 0 --mask $mask"#, $s | dataframe set 0 --mask $mask"#,
result: None, result: None,
}] }]
} }

View File

@ -20,12 +20,8 @@ impl WholeStreamCommand for DataFrame {
fn signature(&self) -> Signature { fn signature(&self) -> Signature {
Signature::build("dataframe sort") Signature::build("dataframe sort")
.optional(
"columns",
SyntaxShape::Table,
"column names to sort dataframe",
)
.switch("reverse", "invert sort", Some('r')) .switch("reverse", "invert sort", Some('r'))
.rest(SyntaxShape::Any, "column names to sort dataframe")
} }
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> { fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
@ -36,7 +32,7 @@ impl WholeStreamCommand for DataFrame {
vec![ vec![
Example { Example {
description: "Create new sorted dataframe", 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, result: None,
}, },
Example { Example {
@ -59,24 +55,23 @@ fn command(mut args: CommandArgs) -> Result<OutputStream, ShellError> {
match value.value { match value.value {
UntaggedValue::DataFrame(PolarsData::EagerDataFrame(df)) => { UntaggedValue::DataFrame(PolarsData::EagerDataFrame(df)) => {
let columns: Option<Vec<Value>> = args.opt(0)?; let columns: Vec<Value> = args.rest(0)?;
match columns { if !columns.is_empty() {
Some(columns) => { let (col_string, col_span) = convert_columns(&columns, &tag)?;
let (col_string, col_span) = convert_columns(&columns, &tag)?;
let res = df let res = df
.as_ref() .as_ref()
.sort(&col_string, reverse) .sort(&col_string, reverse)
.map_err(|e| parse_polars_error::<&str>(&e, &col_span, None))?; .map_err(|e| parse_polars_error::<&str>(&e, &col_span, None))?;
Ok(OutputStream::one(NuDataFrame::dataframe_to_value(res, tag))) Ok(OutputStream::one(NuDataFrame::dataframe_to_value(res, tag)))
} } else {
None => Err(ShellError::labeled_error( Err(ShellError::labeled_error(
"Missing columns", "Missing columns",
"missing column name to perform sort", "missing column name to perform sort",
&tag.span, &tag.span,
)), ))
} }
} }
UntaggedValue::DataFrame(PolarsData::Series(series)) => { UntaggedValue::DataFrame(PolarsData::Series(series)) => {

View File

@ -28,13 +28,13 @@ pub use dataframe::{
DataFrame, DataFrameAggregate, DataFrameAllFalse, DataFrameAllTrue, DataFrameArgMax, DataFrame, DataFrameAggregate, DataFrameAllFalse, DataFrameAllTrue, DataFrameArgMax,
DataFrameArgMin, DataFrameArgSort, DataFrameArgTrue, DataFrameArgUnique, DataFrameColumn, DataFrameArgMin, DataFrameArgSort, DataFrameArgTrue, DataFrameArgUnique, DataFrameColumn,
DataFrameDTypes, DataFrameDrop, DataFrameDropDuplicates, DataFrameDropNulls, DataFrameDummies, DataFrameDTypes, DataFrameDrop, DataFrameDropDuplicates, DataFrameDropNulls, DataFrameDummies,
DataFrameFilter, DataFrameGet, DataFrameGroupBy, DataFrameHead, DataFrameIsDuplicated, DataFrameFilter, DataFrameFirst, DataFrameGet, DataFrameGroupBy, DataFrameIsDuplicated,
DataFrameIsIn, DataFrameIsNotNull, DataFrameIsNull, DataFrameIsUnique, DataFrameJoin, DataFrameIsIn, DataFrameIsNotNull, DataFrameIsNull, DataFrameIsUnique, DataFrameJoin,
DataFrameList, DataFrameLoad, DataFrameMelt, DataFrameNNull, DataFrameNUnique, DataFramePivot, DataFrameLast, DataFrameList, DataFrameMelt, DataFrameNNull, DataFrameNUnique, DataFrameNot,
DataFrameSample, DataFrameSelect, DataFrameSeriesRename, DataFrameSet, DataFrameShift, DataFrameOpen, DataFramePivot, DataFrameSample, DataFrameSelect, DataFrameSeriesRename,
DataFrameShow, DataFrameSlice, DataFrameSort, DataFrameTail, DataFrameToCsv, DataFrameToDF, DataFrameSet, DataFrameShift, DataFrameShow, DataFrameSlice, DataFrameSort, DataFrameToCsv,
DataFrameToParquet, DataFrameToSeries, DataFrameUnique, DataFrameValueCounts, DataFrameWhere, DataFrameToDF, DataFrameToParquet, DataFrameToSeries, DataFrameUnique, DataFrameValueCounts,
DataFrameWithColumn, DataFrameWhere, DataFrameWithColumn,
}; };
pub use env::*; pub use env::*;
pub use filesystem::*; pub use filesystem::*;

View File

@ -268,7 +268,7 @@ pub fn create_default_context(interactive: bool) -> Result<EvaluationContext, Bo
#[cfg(feature = "dataframe")] #[cfg(feature = "dataframe")]
context.add_commands(vec![ context.add_commands(vec![
whole_stream_command(DataFrame), whole_stream_command(DataFrame),
whole_stream_command(DataFrameLoad), whole_stream_command(DataFrameOpen),
whole_stream_command(DataFrameList), whole_stream_command(DataFrameList),
whole_stream_command(DataFrameGroupBy), whole_stream_command(DataFrameGroupBy),
whole_stream_command(DataFrameAggregate), whole_stream_command(DataFrameAggregate),
@ -279,8 +279,8 @@ pub fn create_default_context(interactive: bool) -> Result<EvaluationContext, Bo
whole_stream_command(DataFrameSelect), whole_stream_command(DataFrameSelect),
whole_stream_command(DataFrameDTypes), whole_stream_command(DataFrameDTypes),
whole_stream_command(DataFrameDummies), whole_stream_command(DataFrameDummies),
whole_stream_command(DataFrameHead), whole_stream_command(DataFrameFirst),
whole_stream_command(DataFrameTail), whole_stream_command(DataFrameLast),
whole_stream_command(DataFrameSlice), whole_stream_command(DataFrameSlice),
whole_stream_command(DataFrameMelt), whole_stream_command(DataFrameMelt),
whole_stream_command(DataFramePivot), whole_stream_command(DataFramePivot),
@ -315,6 +315,7 @@ pub fn create_default_context(interactive: bool) -> Result<EvaluationContext, Bo
whole_stream_command(DataFrameIsIn), whole_stream_command(DataFrameIsIn),
whole_stream_command(DataFrameShift), whole_stream_command(DataFrameShift),
whole_stream_command(DataFrameSet), whole_stream_command(DataFrameSet),
whole_stream_command(DataFrameNot),
]); ]);
#[cfg(feature = "clipboard-cli")] #[cfg(feature = "clipboard-cli")]