Dataframe commands (#3608)

* Change name from pls to dataframe

* filter and rename commands

* filter example

* Filter example with bool mask
This commit is contained in:
Fernando Herrera 2021-06-15 03:34:08 +01:00 committed by GitHub
parent d60d71a697
commit ec96e85d04
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
34 changed files with 262 additions and 122 deletions

View file

@ -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;

View file

@ -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,
},
]

View file

@ -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<OutputStream, ShellError> {
@ -31,7 +31,7 @@ impl WholeStreamCommand for DataFrame {
fn examples(&self) -> Vec<Example> {
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,
}]
}

View file

@ -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<OutputStream, ShellError> {

View file

@ -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<Example> {
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,
}]
}

View file

@ -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<Example> {
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,
}]
}

View file

@ -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<Example> {
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,
}]
}

View file

@ -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<OutputStream, ShellError> {
@ -25,7 +25,7 @@ impl WholeStreamCommand for DataFrame {
fn examples(&self) -> Vec<Example> {
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,
}]
}

View file

@ -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<OutputStream, ShellError> {
@ -27,7 +27,7 @@ impl WholeStreamCommand for DataFrame {
fn examples(&self) -> Vec<Example> {
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,
}]
}

View file

@ -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<OutputStream, ShellError> {
command(args)
}
fn examples(&self) -> Vec<Example> {
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<OutputStream, ShellError> {
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)))
}

View file

@ -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<Example> {
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,
}]
}

View file

@ -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<Example> {
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,
}]
}

View file

@ -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<Example> {
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,
}]
}

View file

@ -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,
},
]

View file

@ -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<OutputStream, ShellError> {
@ -57,7 +57,7 @@ impl WholeStreamCommand for DataFrame {
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Lists loaded dataframes in current scope",
example: "pls list",
example: "dataframe list",
result: None,
}]
}

View file

@ -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<Example> {
vec![Example {
description: "Takes a file name and creates a dataframe",
example: "pls load test.csv",
example: "dataframe load test.csv",
result: None,
}]
}

View file

@ -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<Example> {
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,
}]
}

View file

@ -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;

View file

@ -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,
}]
}

View file

@ -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,
},
]

View file

@ -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<Example> {
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,
}]
}

View file

@ -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<OutputStream, ShellError> {
command(args)
}
fn examples(&self) -> Vec<Example> {
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<OutputStream, ShellError> {
let tag = args.call_info.name_tag.clone();
let name: Tagged<String> = 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)))
}

View file

@ -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,
},
]

View file

@ -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<Example> {
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,
}]
}

View file

@ -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<Example> {
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,
}]
}

View file

@ -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<Example> {
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,
}]
}

View file

@ -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,
},
]

View file

@ -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<OutputStream, ShellError> {
@ -29,7 +29,7 @@ impl WholeStreamCommand for DataFrame {
fn examples(&self) -> Vec<Example> {
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,
}]
}

View file

@ -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<Example> {
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,
}]
}

View file

@ -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<Example> {
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,
}]
}

View file

@ -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<Example> {
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,
}]
}

View file

@ -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<OutputStream, ShellError> {
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)))
}

View file

@ -288,6 +288,8 @@ pub fn create_default_context(interactive: bool) -> Result<EvaluationContext, Bo
whole_stream_command(DataFrameDropNulls),
whole_stream_command(DataFrameColumn),
whole_stream_command(DataFrameWithColumn),
whole_stream_command(DataFrameFilter),
whole_stream_command(DataFrameSeriesRename),
]);
#[cfg(feature = "clipboard-cli")]

View file

@ -79,7 +79,8 @@ impl NuSeries {
match value.value {
UntaggedValue::Primitive(Primitive::Int(_))
| UntaggedValue::Primitive(Primitive::Decimal(_))
| UntaggedValue::Primitive(Primitive::String(_)) => {
| 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::<Vec<Value>>()
@ -285,6 +282,10 @@ fn insert_value(value: Value, vec_values: &mut Vec<Value>) -> 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<_>, _> = 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"),
};